Differences between current version and predecessor to the previous major change of adler32.sh.

Other diffs: Previous Revision, Previous Author

Newer page: version 2 Last edited on Saturday, 24 July 2010 18:28:53 by CyberLeo
Older page: version 1 Last edited on Friday, 14 November 2008 7:59:34 by CyberLeo Revert
@@ -1,5 +1,5 @@
-<verbatim
+<code brush="bash"
 # Ordinal functions from here: 
 #http://developer.apple.com/documentation/OpenSource/Conceptual/ShellScripting/AnExtremeExample/chapter_951_section_3.html 
 inttooct() { 
  echo $(echo "obase=8; $1" | bc) 
@@ -100,5 +100,5 @@
  printf "%08X\n" $(( (${b} * 256) + ${a} )) 
 
  
 adler32 "$(hostname)" 
-</verbatim
+</code

current version

# Ordinal functions from here:
#http://developer.apple.com/documentation/OpenSource/Conceptual/ShellScripting/AnExtremeExample/chapter_951_section_3.html
inttooct() {
  echo $(echo "obase=8; $1" | bc)
}
ord_init(){
  I=1
  ORDSTRING=""
  while [ $I -lt 256 ] ; do
    local OCT=$(inttooct $I);
    local CH=$(echo ' ' | tr ' ' "\\$OCT");
    ORDSTRING=$ORDSTRING$CH
    I=$(($I + 1)) # or I=$(expr $I '+' 1)
  done
}
ord()
{
  local CH="$1"
  local STRING=""
  local OCCOPY=$ORDSTRING
  local COUNT=0;

  # Delete the first character from a copy of ORDSTRING if that
  # character doesn't match the one we're looking for.  Loop
  # until we don't have any more leading characters to delete.
  # The count will be the ASCII character code for the letter.
  CONT=1;
  while [ $CONT = 1 ]; do
    # Copy the string so we know if we've stopped finding
    # non-matching characters.
    OCTEMP="$OCCOPY"

    # If it's a close bracket, quote it; we don't want to
    # break the regexp.
    if [ "x$CH" = "x]" ] ; then
        CH='\]'
    fi

    # Delete a character if possible.
    OCCOPY=$(echo "$OCCOPY" | sed "s/^[^$CH]//");

    # On error, we're done.
    if [ $? != 0 ] ; then CONT=0 ; fi

    # If the string didn't change, we're done.
    if [ "x$OCTEMP" = "x$OCCOPY" ] ; then CONT=0 ; fi

    # Increment the counter so we know where we are.
    COUNT=$((COUNT + 1)) # or COUNT=$(expr $COUNT '+' 1)
    # echo "COUNT: $COUNT"
  done

  COUNT=$(($COUNT + 1)) # or COUNT=$(expr $COUNT '+' 1)
  # If we ran out of characters, it's a null (character 0).
  if [ "x$OCTEMP" = "x" ] ; then COUNT=0; fi

  # Return the ord of the character in question....
  echo $COUNT
  # exit 0
}

firstchar() {
  eval $(echo -en "${data}" | sed -e 's/^\(.\)\(.*\)$/char=$(ord "\1") data="\2"/g')
}

adler32(){
  if [ -z "${1}" ]
  then
    echo "Usage: adler32 'data'"
    echo "Calculates adler32 checksum on data."
    return 1
  fi
  data="${1}"
  len=$(echo "${data}" | /usr/bin/wc -c)
  a=1
  b=0
  ord_init
  while [ "${len}" -gt 0 ]
  do
    if [ "${len}" -gt 5552 ]
    then
      tlen=5552
    else
      tlen=${len}
    fi
    len=$(( ${len} - ${tlen} ))

    while [ "${tlen}" -gt 0 ]
    do
      firstchar
      a=$(( ${a} + ${char} ))
      b=$(( ${b} + ${a} ))
      tlen=$(( ${tlen} - 1 ))
    done
    a=$(( ${a} % 65521 ))
    b=$(( ${b} % 65521 ))
  done

  printf "%08X\n" $(( (${b} * 256) + ${a} ))
}

adler32 "$(hostname)"