Guest User

Check SWF for ASC 2.0 Type Coercion Bug

a guest
Aug 13th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.82 KB | None | 0 0
  1. #!/bin/bash
  2. set -o nounset
  3.  
  4. if [ "$#" -ne 2 ]; then
  5.     echo -e "Usage:\n\t$0 <fla file OR folder containing xfl> <swf file>\n"
  6.     exit 1;
  7. fi
  8.  
  9. FLA_PATH=$(pwd)/$(basename $1)
  10.  
  11. SWF=$(basename "$2")
  12. SWF_PATH=$(pwd)/$SWF
  13.  
  14. count_asset_named_placements()
  15. {
  16.     ASSET_CLASS=$1
  17.     DUMP_FILE=$2
  18.  
  19.     EXPORT_ID=$(grep "exports [0-9]\+ as \"$ASSET_CLASS\"" $DUMP_FILE | awk '{ print $2 }')
  20.  
  21.     if [ -z "$EXPORT_ID" ]
  22.     then
  23.         echo 0
  24.         exit
  25.     fi
  26.  
  27.     grep "places id $EXPORT_ID .* name " $DUMP_FILE |  wc | awk '{ print $1 }'
  28. }
  29.  
  30. SWF_DUMP_TMP=$(mktemp)
  31.  
  32. swfdump -u "$SWF_PATH" > $SWF_DUMP_TMP
  33.  
  34. echo "Swfdump: $SWF_DUMP_TMP"
  35.  
  36. if [ -d $FLA_PATH ]
  37. then
  38.     pushd $FLA_PATH > /dev/null
  39.     DELETE_FLA_TMP_FOLDER=false
  40. else
  41.     FLA_TMP_FOLDER=$(mktemp -d "${TMPDIR:-/tmp/}"$(basename $0).XXXXXX)
  42.     DELETE_FLA_TMP_FOLDER=true
  43.  
  44.     pushd $FLA_TMP_FOLDER > /dev/null
  45.     unzip -qq "$FLA_PATH" 2>/dev/null
  46. fi
  47.  
  48. BUILD_BAD=false
  49.  
  50. FLA_ASSETS=$(find LIBRARY -name '*.xml' -exec grep -l linkageClassName {} \+ | sed 's/^LIBRARY\/\(.*\)\.xml/\1/')
  51.  
  52. #
  53. # First perform some useful analysis
  54. #
  55.  
  56. declare -A ASSET_TO_CLASS
  57. declare -A CLASS_TO_ASSET
  58. declare -A IS_ASSET_LINKED
  59. declare -A IS_CLASS_LINKED
  60.  
  61. IFS=$'\n'
  62. for ASSET in $FLA_ASSETS
  63. do
  64.     CLASS=$(grep linkageClassName "LIBRARY/${ASSET}.xml" | sed 's/.*linkageClassName="\([^"]*\)".*/\1/')
  65.  
  66.     ASSET_TO_CLASS[$ASSET]=$CLASS
  67.     CLASS_TO_ASSET[$CLASS]=$ASSET
  68.  
  69.     LINKED=$(grep "exports .*$CLASS" $SWF_DUMP_TMP | wc | awk '{ print $1 }')
  70.  
  71.     IS_ASSET_LINKED[$ASSET]=$LINKED
  72.     IS_CLASS_LINKED[$CLASS]=$LINKED
  73. done
  74.  
  75.  
  76. for FLA_ASSET in $FLA_ASSETS
  77. do
  78.     #echo -e "Asset: $FLA_ASSET\n  Class: ${ASSET_TO_CLASS[$FLA_ASSET]}\n  Linked: ${IS_ASSET_LINKED[$FLA_ASSET]}"
  79.  
  80.     #
  81.     # If the asset isn't linked into the swf, check if it should be. For example sometimes the AS3 component skins are simply missing
  82.     # if they aren't otherwise referenced by linkage class in the source.
  83.     #
  84.  
  85.     if [ "${IS_ASSET_LINKED[$FLA_ASSET]}" == 0 ]
  86.     then
  87.         PLACED_BY_FLA_ASSETS=$(find LIBRARY -name '*.xml' -exec grep "libraryItemName=\"$FLA_ASSET\"" {} \+ | grep Instance | sed 's/^LIBRARY\/\(.*\)\.xml.*/\1/' | sort -u)
  88.  
  89.         for PLACED_BY_FLA_ASSET in $PLACED_BY_FLA_ASSETS
  90.         do
  91.             if [ "${IS_ASSET_LINKED[$PLACED_BY_FLA_ASSET]}" == 1 ]
  92.             then
  93.                 echo "ERROR: $FLA_ASSET is not linked but is placed by linked asset $PLACED_BY_FLA_ASSET" 1>&2
  94.                 BUILD_BAD=true
  95.             fi
  96.         done
  97.  
  98.         continue
  99.     fi
  100.  
  101.     #
  102.     # Sometimes an asset will be linked but wont be correctly associated with instances given a name at design time.
  103.     #
  104.  
  105.     NAMED_BY_FLA_ASSETS=$(find LIBRARY -name '*.xml' -exec grep "libraryItemName=\"$FLA_ASSET\"" {} \+ | grep Instance | grep name=\" | sed 's/^LIBRARY\/\(.*\)\.xml.*/\1/' | sort -u)
  106.  
  107.     if [ -z "$NAMED_BY_FLA_ASSETS" ]
  108.     then
  109.         continue
  110.     fi
  111.  
  112.     NAMED_USES=0
  113.  
  114.     for NAMED_BY_FLA_ASSET in $NAMED_BY_FLA_ASSETS
  115.     do
  116.         if [ "${IS_ASSET_LINKED[$NAMED_BY_FLA_ASSET]}" == 1 ]
  117.         then
  118.             #echo "  named by linked asset $NAMED_BY_FLA_ASSET"
  119.             NAMED_USES=$((NAMED_USES + 1))
  120.         fi
  121.     done
  122.  
  123.     if [ "$NAMED_USES" == 0 ]
  124.     then
  125.         continue
  126.     fi
  127.  
  128.     #
  129.     # The asset is named at least once in another asset that is linked into the swf.
  130.     #
  131.  
  132.     FLA_ASSET_CLASS=${ASSET_TO_CLASS[$FLA_ASSET]}
  133.  
  134.     SWF_USES=$(count_asset_named_placements $FLA_ASSET_CLASS $SWF_DUMP_TMP)
  135.  
  136.     if [ "$SWF_USES" != 0 ] || [ "$NAMED_USES" != 0 ]
  137.     then
  138.         if [ "$SWF_USES" != "$NAMED_USES" ]
  139.         then
  140.             echo "ERROR: $FLA linked as $FLA_ASSET_CLASS with $NAMED_USES expected named uses, found $SWF_USES named uses in swf" 1>&2
  141.             BUILD_BAD=true
  142.         #else
  143.             #echo "  linked as $FLA_ASSET_CLASS with $NAMED_USES expected named uses, found $SWF_USES named uses in swf"
  144.         fi
  145.     fi
  146. done
  147.  
  148. popd > /dev/null
  149.  
  150. #
  151. # Cleanup
  152. #
  153.  
  154. if $DELETE_FLA_TMP_FOLDER
  155. then
  156.     rm -rf "$FLA_TMP_FOLDER"
  157. fi
  158.  
  159. if $BUILD_BAD
  160. then
  161.     echo -e "\nBuild is bad."
  162.  
  163.     exit 2
  164. else
  165.     echo -e "\nBuild is good!"
  166. fi
Advertisement
Add Comment
Please, Sign In to add comment