Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String encoding not working #139

Open
Dinhero21 opened this issue Nov 7, 2022 · 1 comment
Open

String encoding not working #139

Dinhero21 opened this issue Nov 7, 2022 · 1 comment

Comments

@Dinhero21
Copy link

I am trying to encode a utf16 string in protodef but when I do

"string": [
  "pstring", {
    "countType": "u16"
  }
]

it will encode a utf8 string and when I add

"string": [
  "pstring", {
    "countType": "u16",
    "encoding": "utf16"
  }
]

it will just give a validation error

Here is the validation error in case you need it:

{
  "keyword": "enum",
  "dataPath": "",
  "schemaPath": "#/oneOf/0/enum",
  "params": {
    "allowedValues": [
      "native"
    ]
  },
  "message": "should be equal to one of the allowed values",
  "schema": [
    "native"
  ],
  "parentSchema": {
    "enum": [
      "native"
    ]
  },
  "data": [
    "pstring",
    {
      "countType": "u16",
      "encoding": "utf16"
    }
  ]
}
@Dinhero21
Copy link
Author

I fixed my issue by creating my custom implementation:

function read (buffer: Buffer, offset: number): { value: string, size: number } {
  buffer = buffer.subarray(offset)

  const uint16Array = bufferToUint16Array(buffer)

  const length = uint16Array[0]

  const messageUint16Array = uint16Array.subarray(1, length + 1)

  const messageBuffer = uint16ArrayToBuffer(messageUint16Array)
  messageBuffer.swap16()

  const message = messageBuffer.toString('utf16le')

  return {
    value: message,
    size: size(message)
  }
}

function write (value: string, buffer: Buffer, offset: number): number {
  const messageBuffer = Buffer.from(value, 'utf16le')
  messageBuffer.swap16()

  const messageUint16Array = bufferToUint16Array(messageBuffer)

  const length = value.length
  const uint16Array = new Uint16Array([length, ...messageUint16Array])

  const encodedBuffer = uint16ArrayToBuffer(uint16Array)

  encodedBuffer.copy(buffer, offset)

  return offset + encodedBuffer.length
}

function size (value: string): number {
  return (value.length + 1) * 2
}

It fixes my problem but it would still be nice to have the option to do it natively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant