help-bash
[Top][All Lists]
Advanced

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

Re: Help fixing NativeMessaging host: read 32-bit message length in nati


From: alex xmb ratchev
Subject: Re: Help fixing NativeMessaging host: read 32-bit message length in native byte order
Date: Mon, 26 Jun 2023 14:27:18 +0200

On Sat, Jun 24, 2023, 15:34 guest271314 <guest271314@gmail.com> wrote:

> 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[@]}"
> }
>

u realize .. -N 1 to read may read 0 - 9 , one number , not 32 or anything
else but ' 1 char / number '
or am i lost ..

>


reply via email to

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