0

I have a simple script to automatically download Android firmware and decrypt it. However I need to use correct decryption type of the file enc2 or enc4.

Here is a most likely the script I will write:

#!/bin/bash

# Check update
samloader -m GT-I8190N -r BTU checkupdate

# Download
samloader -m GT-I8190N -r BTU download -v I8190NXXAMJ2/I8190NBTUAMJ1/I8190NXXAMJ2/I8190NXXAMJ2 -O .


# Decrypt
#
# THIS IS THE PROBLEM
samloader -m GT-I8190N -r BTU decrypt -v I8190NXXAMJ2/I8190NBTUAMJ1/I8190NXXAMJ2/I8190NXXAMJ2 **-V 2** -i GT-I8190N_BTU_1_20131118100230_9ae3yzkqmu_fac.zip.**enc2** -o GT-I8190N_BTU_1_20131118100230_9ae3yzkqmu_fac.zip

Take a look at the code with (** **)

The decrypted file is either *.enc2 or *.enc4. My problem is how can I detect the format (2 or 4)?. Like using sed, or awk, etc.

If enc2, then I specify -V 2 as well as enc4, -V 4

Thanks! Let me know if you need more info

The script: https://github.com/nlscc/samloader

  • This answer shows how to get last character of a string (which would be your filename variable): https://stackoverflow.com/a/17542946/2711811. –  May 13 '21 at 02:27
  • 1
    Bash has all you need. `ext="${fname##*.}"` will save the extension in `ext`. Then just `case "$ext" in enc2 ) ## do for enc2;; enc4 ) ## do for enc4;; * ) ## error unmatched ext;; esac` Or simply use `if [ "$ext" = "enc2" ]; then ## do for enc2 elif [ "$ext" = "enc4" ]; then ## do for enc4 else ## unmatched fi` (note the `##` are just comments where you would provide the necessary expressions) – David C. Rankin May 13 '21 at 02:43
  • @David C. Rankin thanks! This is what I looking for – hendramanudinata03 May 13 '21 at 03:06
  • @Andy i'm sorry, but I couldn't found the answer for my question in that answer. Can you show me which one? – hendramanudinata03 May 13 '21 at 03:06
  • @David C. Rankin Can you add that to answer? – hendramanudinata03 May 13 '21 at 03:07
  • 1
    Okay, give me a sec and I'll write it up. – David C. Rankin May 13 '21 at 03:08

2 Answers2

1

When you need to isolate an extension from a filename, you can use the parameter expansion with substring removal provided by bash. The substring removals can trim from either the left or right of a string (beginning or end). They are:

    ${var#pattern}      Strip shortest match of pattern from front of $var
    ${var##pattern}     Strip longest match of pattern from front of $var
    ${var%pattern}      Strip shortest match of pattern from back of $var
    ${var%%pattern}     Strip longest match of pattern from back of $var

So for a filename, you can isolate the extension with:

ext="${fname##*.}"

To strip all characters from the front of fname up to and including the last '.' leaving only the extension saved in ext.

Then you can either use a if..elif..else..fi or case...esac statement to provide the correct decryption based on the extension. For example, using if, you can do:

if [ "$ext" = "enc2" ]; then
    ## samloader with -V 2 command
elif [ "$ext" = "enc4" ]; then
    ## samloader with -V 4 command
else
    printf "error: unmatched extension.\n" >&2
    exit 1
fi

or with a case statement you would use:

case "$ext" in
    "enc2" ) ## samloader with -V 2 command
            ;;
    "enc4" ) ## samloader with -V 4 command
            ;;
       *   )
            printf "error: unmatched extension.\n" >&2
            ;;
 esac

(note: you don't actually have to separate the extension from the filename first before calling the case statement as you can use *enc2 and *enc4 to operate on the entire filename. You can use similar wildcards with if..., but I find it better for troubleshooting to explicitly extract the extension and then operate on it specifically -- up to you)

If you want to build the complete command to execute assigning -V 2 or -V 4, then see: BashFAQ #50, and specifically 5. I'm constructing a command based on information that is only known at run time

Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

You actually don't even need to isolate the extension part of the name. Just:

# well you don't actually need to quote after `case`...
case "$fname" in
  (*.sam2) V=2;;
  (*.sam4) V=4;;
  (*)  printf "error: unmatched extension.\n" >&2; exit 1;;
esac

samloader -m GT-I8190N -r BTU decrypt -v I8190NXXAMJ2/I8190NBTUAMJ1/I8190NXXAMJ2/I8190NXXAMJ2 \
  -V "$V" -i "$fname" -o "${fname%.*}"

(Well, apparently you still need to isolate the name without the enc part. Fine.)


By the way, I am pretty sure a lack of auto-detection is a very worthy feature request for samloader.

Mingye Wang
  • 1,107
  • 9
  • 32