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: guest271314
Subject: Re: Help fixing NativeMessaging host: read 32-bit message length in native byte order
Date: Sat, 24 Jun 2023 10:30:46 -0700

It looks like 2047 is the breaking point for some reason.

port.postMessage('1'.repeat(2044));

1.111111111111111e+253 is Logged 7 times. 254 is in test.txt

port.postMessage('1'.repeat(2043));

111111...1, the expectd result, is logged, 2045 is in test.txt

#!/bin/bash
# Loop forever, to deal with chrome.runtime.connectNative
getMessage() {
    IFS= read -rN"$1" message
    echo -n "$message" >> data.txt
    sendMessage "$message"
}
sendMessage() {
  message="$*"
  # Calculate the byte size of the string.
  # NOTE: This assumes that byte length is identical to the string length!
  # Do not use multibyte (unicode) characters, escape them instead, e.g.
  # message='"Some unicode character:\u1234"'
  messagelen=${#message}
  # Convert to an integer in native byte order.
  # If you see an error message in Chrome's stdout with
  # "Native Messaging host tried sending a message that is ... bytes long.",
  # then just swap the order, i.e. messagelen1 <-> messagelen4 and
  # messagelen2 <-> messagelen3
  messagelen1=$(((messagelen) & 0xFF))
  messagelen2=$(((messagelen >> 8) & 0xFF))
  messagelen3=$(((messagelen >> 16) & 0xFF))
  messagelen4=$(((messagelen >> 24) & 0xFF))
  # Print the message byte length followed by the actual message.
  printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \
    $messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message"
}

main() {
  $(> length.txt)
  $(> test.txt)
  $(> data.txt)
  read -n1 uint32
  echo -n "$uint32" >> length.txt
  length=$(cat length.txt | od -An -td4 -)
  length=$((length))      # trim leading spaces
  echo $length >> test.txt
  while true; do
    getMessage $length
  done
}

main

On Sat, Jun 24, 2023 at 8:21 AM Greg Wooledge <greg@wooledge.org> wrote:

> On Sat, Jun 24, 2023 at 08:10:39AM -0700, guest271314 wrote:
> > So, in the end, you can use something like this.
> >
> >   length=$(dd iflag=fullblock bs=4 count=1 | od -An -td4)
> >   length=$((length))      # trim leading spaces
> >   IFS= read -rN"$length" json
> >
> > That still winds up showing "Invalid byte sequence in conversion input"
> > when echo'ing the output to a file.
>
> Please show us the input.  Since it's binary, you'll need to represent
> it in some way that can fit into an e-mail body.  Perhaps a hex dump of
> the first 128 bytes or something.
>
> I'm rather curious what possible input could be an "invalid byte sequence"
> when interpreted as 32 bits that make up an integer.  Is it having the
> high bit set (meaning negative when read as a signed integer)?
>
> unicorn:~$ printf '\x00\x00\x00\x80' | od -An -td4
>  -2147483648
>
> Hmm, no, that looks all right to me.  Certainly didn't give that error.
>
> What operating system and architecture are you on, and what input are you
> using to generate this message, and which program is actually writing it?
>
>


reply via email to

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