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

Replace the functionality of xxd #4

Open
SDuesterhaupt opened this issue Feb 18, 2020 · 7 comments
Open

Replace the functionality of xxd #4

SDuesterhaupt opened this issue Feb 18, 2020 · 7 comments

Comments

@SDuesterhaupt
Copy link

xxd is hard to get in distributions like CentOS.

Maybe it's better to include an alternative functionality like this:

hex2string() {
  MyRun=true
  while $MyRun; do
    if [ ! -z "$1" ]; then
      MyPart=$1
      MyRun=false
    else
      read MyPart
      MyTest=$?
      if [ $MyTest -ne 0 ]; then
        MyRun=false
      fi
    fi
    I=0
    while [ $I -lt ${#MyPart} ];
    do
      echo -en "\x"${MyPart:$I:2}
      let "I += 2"
    done
  done
}

The usage is for example in the function key_get_modulus():

#sed -e 's/^Modulus=//' < "$OPENSSL_OUT" \
# | xxd -r -p \
# | base64url
sed -e 's/^Modulus=//' < "$OPENSSL_OUT" \
 | hex2string \
 | base64url
@bruncsak
Copy link
Owner

bruncsak commented Feb 21, 2020

It is very easy to get xxd for CentOS, just do as root:
yum install vim-common

On the other hand I support your idea to replace xxd with standard shell code sequence.

The problem with your code that it runs only on recent bash. It does not run on ksh, not even talking about xpg4 or svr4 standard only shells. I already made effort to backport my code to support Tru64 UNIX and Solaris.

@SDuesterhaupt
Copy link
Author

SDuesterhaupt commented Feb 22, 2020

27 M vim-common installation size for using only this function? This was the reason to think about an alternative.

You are right, the code is developed and tested for the bash shell. I concentrate on RedHat/CentOS.

And this code is an example. The development state is from 2016. In the meanwhile I develop shell code under consideration of POSIX (mostly, sometimes it's a hard business).

What I can see immediately is the command let "I += 2" which is definitely not POSIX compatible. Today I would use such a construction:

I="$((I+2))"

I will revise this part.

@bruncsak
Copy link
Owner

  • the built in echo of the ksh does not know the \x<hexdigit><hexdigit> escape sequence.
  • the POSIX shell on Tru64 UNIX fails on ${MyPart:$I:2}. That looks like a double shell variable expansion.

@SDuesterhaupt
Copy link
Author

SDuesterhaupt commented Feb 22, 2020

  • the POSIX shell on Tru64 UNIX fails on ${MyPart:$I:2}. That looks like a double shell variable expansion.

It's a command construction to expand a substring of a given parameter: ${parameter:offset:length}.

The parameter is in our case 1 because we have only one parameter ($1).

To substitute this under consideration of POSIX we can maybe use a construction like this:

echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/'

And within the test function __hex2string2 ():

function __hex2string2 () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/')
    I="$((I+2))"
  done
}

The call sequence is:

__hex2string2 "48616c6c6f2057656c74210a"

For sure it's not the be-all and end-all. In this example for example the command construction takes always the whole length of the parameter $1.

Further ideas?

@bruncsak
Copy link
Owner

That is very good. I think the other problem is harder. Do we have to do some hexadecimal -> octal math?

@bruncsak
Copy link
Owner

I committed the fix: 1f408c5

@SDuesterhaupt
Copy link
Author

SDuesterhaupt commented Feb 23, 2020

  • the built in echo of the ksh does not know the \x<hexdigit><hexdigit> escape sequence.

I just love scripting in Linux... 😍 Two alternatives which seem to solve this issue and run also in ksh:

#!/bin/ksh

__hex2string2 () {
  I=0
  while [ "$I" -lt "${#1}" ];
  do
    #echo -en "\x"$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/')
    #printf '\x'$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/')

    echo -en "$(printf '\x'$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/'))"

    I="$((I+2))"
  done
}

__hex2string3 () {
  I=0
  while [ "$I" -lt "${#1}" ];
  do
    #echo -en "\x"$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/')

    MyHEX=$(echo $1 | sed 's/^.\{'$I'\}\(.\{2\}\).*/\1/')
    #echo "$MyHEX"
    MyDECIMAL="$((16#$MyHEX))"
    typeset -i8 MyOCT=MyDECIMAL               # typeset adds the base '8#' 
    #echo "$MyOCT"
    MyOCT2=$(echo "$MyOCT" | sed 's/8\#//g')  # remove the base '8#'
    #echo "$MyOCT2"
    MyCHAR=$(echo "\0${MyOCT2}")
    echo -en "${MyCHAR}"

    I="$((I+2))"
  done
}


__hex2string2 "48616c6c6f2057656c74210a"

echo -e "\n"

__hex2string3 "48616c6c6f2057656c74210a"

exit 0

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

No branches or pull requests

2 participants