help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help fixing NativeMessaging host: read 32-bit message length in native b


From: guest271314
Subject: Help fixing NativeMessaging host: read 32-bit message length in native byte order
Date: Fri, 23 Jun 2023 22:20:33 -0700

I have been trying to write a Native Messaging host in Bash comparable to
C, C++, Python, and JavaScript versions, trying to determine if Bash is
capable of doing so after reading
https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application.
This is the Native messaging protocol
https://developer.chrome.com/docs/extensions/mv3/nativeMessaging/#native-messaging-host-protocol
.

> Chrome starts each native messaging host in a separate process and
communicates with it using standard input (stdin) and standard output
(stdout). The same format is used to send messages in both directions; each
message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit
message length in native byte order. The maximum size of a single message
from the native messaging host is 1 MB, mainly to protect Chrome from
misbehaving native applications. The maximum size of the message sent to
the native messaging host is 4 GB.

This is what I came up for getMessage
https://github.com/guest271314/native-messaging-bash/blob/main/nm_bash.sh,
the part I need help with is reading message length. In C, C++, Python,
JavaScript I can pass new Array(209715) from JavaScript (1 MB) to the host
and get the message echo'ed back. In the code below new Array(408) (JSON
length 2041) is echo'ed back, over that input length no message is echo'ed
back. How do I reliably read 32-bit message length in native byte order
using GN Core Utilities in Bash?

getMessage() {
  read -N 1 uint32
  # https://unix.stackexchange.com/a/13141
  header=0x$(printf "%s" "$uint32" |
  od -t x8 -An |
  tr -dc '[:alnum:]')
  messageLength=$(printf "%d" "$header")
  array=()
  read -N "$messageLength" json
  array+=("$json")
  sendMessage "${array[@]}"
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]