File: md5check.sh

package info (click to toggle)
moc 1%3A2.6.0~svn-r3005-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,656 kB
  • sloc: ansic: 31,749; sh: 929; cpp: 487; makefile: 240
file content (311 lines) | stat: -rwxr-xr-x 8,334 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash

#
# MOC - music on console
# Copyright (C) 2004-2005 Damian Pietras <daper@daper.net>
#
# md5check.sh Copyright (C) 2012 John Fitzgerald
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

#
# TODO: - Make format, rate and channels explicit where possible.
#       - Add remaining decoder check functions.
#       - Fix decoders with genuine MD5 mismatches.
#       - Fix decoders with genuine format mismatches.
#       - Handle format mismatches which aren't genuine.
#       - Possibly rewrite in Perl for speed.
#

declare -A UNKNOWN
declare -A UNSUPPORTED

EXTRA=false
IGNORE=false
RC=0
SILENT=false
TREMOR=false
VERBOSE=false

# Clean error termination.
function die {
  echo '***' $@ > /dev/stderr
  exit 1
}

# Provide usage information.
function usage () {
  echo "Usage: ${0##*/} [-e] [-s|-v] [LOGFILE]"
  echo "       ${0##*/} -h"
}

# Provide help information.
function help () {
  echo
  echo "MOC MD5 sum checking tool"
  echo
  usage
  echo
  echo "  -e|--extra      Perform extra checks"
  echo "  -h|--help       This help information"
  echo "  -i|--ignore     Ignore known problems"
  echo "  -s|--silent     Output no results"
  echo "  -v|--verbose    Output all results"
  echo
  echo "  LOGFILE         MOC server log file name, or '-' for stdin"
  echo "                  (default: 'mocp_server_log' in the current directory)"
  echo
  echo "Exit codes:       0 - No errors or mismatches"
  echo "                  1 - Error occurred"
  echo "                  2 - Mismatch found"
  echo
}

# Check the FAAD decoder's samples.
FAAD=$(which faad 2>/dev/null)
function aac () {
  local ENDIAN OPTS

  [[ -x "$FAAD" ]] || die faad2 not installed

  [[ "${FMT:0:1}" = "f" ]] && ENDIAN=le
  OPTS="-w -q -f2"

  [[ "${FMT:1:2}" = "16" ]] && OPTS="$OPTS -b1"
  [[ "${FMT:1:2}" = "24" ]] && OPTS="$OPTS -b2"
  [[ "${FMT:1:2}" = "32" ]] && OPTS="$OPTS -b3"

  SUM2=$($FAAD $OPTS "$FILE" | md5sum)
  LEN2=$($FAAD $OPTS "$FILE" | wc -c)
}

# Check the FFmpeg decoder's samples.
FFMPEG=$(which avconv 2>/dev/null || which ffmpeg 2>/dev/null)
function ffmpeg () {
  local ENDIAN OPTS

  [[ -x "$FFMPEG" ]] || die ffmpeg/avconv not installed

  [[ "${FMT:0:1}" = "f" ]] && ENDIAN=le
  OPTS="-ac $CHANS -ar $RATE -f $FMT$ENDIAN"

  SUM2="$($FFMPEG -i "$FILE" $OPTS - </dev/null 2>/dev/null | md5sum)"
  LEN2=$($FFMPEG -i "$FILE" $OPTS - </dev/null 2>/dev/null | wc -c)

  [[ "$($FFMPEG -i "$FILE" </dev/null 2>&1)" =~ Audio:\ .*\ (mono|stereo) ]] || \
    IGNORE_SUM=$IGNORE
}

# Check the FLAC decoder's samples.
SOX=$(which sox 2>/dev/null)
function flac () {
  local OPTS

  [[ -x "$SOX" ]] || die "SoX (for flac) not installed"

  [[ "${FMT:0:1}" = "s" ]] && OPTS="-e signed" || OPTS="-e unsigned"
  [[ "${FMT:1:1}" = "8" ]] && OPTS="$OPTS -b8 -L"
  [[ "${FMT:1:2}" = "16" ]] && OPTS="$OPTS -b16"
  [[ "${FMT:1:2}" = "24" ]] && OPTS="$OPTS -b24"
  [[ "${FMT:1:2}" = "32" ]] && OPTS="$OPTS -b32"
  [[ "$FMT" =~ "le" ]] && OPTS="$OPTS -L"
  [[ "$FMT" =~ "be" ]] && OPTS="$OPTS -B"
  OPTS="$OPTS -r$RATE -c$CHANS"

  SUM2=$($SOX "$FILE" $OPTS -t raw - | md5sum)
  LEN2=$($SOX "$FILE" $OPTS -t raw - | wc -c)
}

# Check the Ogg/Vorbis decoder's samples.
OGGDEC=$(which oggdec 2>/dev/null)
function vorbis () {
  [[ -x "$OGGDEC" ]] || die oggdec not installed
  SUM2="$($OGGDEC -RQ -o - "$FILE" | md5sum)"
  LEN2=$($OGGDEC -RQ -o - "$FILE" | wc -c)
}

# Check the LibSndfile decoder's samples.
SOX=$(which sox 2>/dev/null)
function sndfile () {
  # LibSndfile doesn't have a decoder, use SoX.
  [[ -x "$SOX" ]] || die "sox (for sndfile) not installed"
  SUM2="$($SOX "$FILE" -t f32 - | md5sum)"
  LEN2=$($SOX "$FILE" -t f32 - | wc -c)
  [[ "$NAME" == *-s32le-* ]] && IGNORE_SUM=$IGNORE
}

# Check the MP3 decoder's samples.
SOX=$(which sox 2>/dev/null)
function mp3 () {
  # Lame's decoder only does 16-bit, use SoX.
  [[ -x "$SOX" ]] || die "sox (for mp3) not installed"
  SUM2="$($SOX "$FILE" -t s32 - | md5sum)"
  LEN2=$($SOX "$FILE" -t s32 - | wc -c)
  IGNORE_SUM=$IGNORE
  IGNORE_LEN=$IGNORE
}

# Check the Speex decoder's samples.
SPEEX=$(which speexdec 2>/dev/null)
function speex () {
  [[ -x "$SPEEX" ]] || die speexdec not installed
  SUM2="$($SPEEX "$FILE" - 2>/dev/null | md5sum)"
  LEN2=$($SPEEX "$FILE" - 2>/dev/null | wc -c)
  IGNORE_SUM=$IGNORE
  IGNORE_LEN=$IGNORE
}

# Process command line options.
for OPTS
do
  case $1 in
    -e|--extra) EXTRA=true
                ;;
   -i|--ignore) IGNORE=true
                ;;
  -v|--verbose) VERBOSE=true
                SILENT=false
                ;;
   -s|--silent) SILENT=true
                VERBOSE=false
                ;;
     -h|--help) help
                exit 0
                ;;
          --|-) break
                ;;
            -*) echo Unrecognised option: $1
                usage > /dev/stderr
                exit 1
                ;;
             *) break
                ;;
  esac
  shift
done

# Allow for log file parameter.
LOG="${1:-mocp_server_log}"
[[ "$LOG" = "-" ]] && LOG=/dev/stdin

# Output formatting.
$SILENT || echo

# Process server log file.
while read
do

  # Reject log file if circular logging has been used.
  [[ "$REPLY" =~ "Circular Log Starts" ]] && \
      die MD5 sums cannot be checked when circular logging was used

  # Extract MOC revision header.
  [[ "$REPLY" =~ "This is Music On Console" ]] && \
      REVN="$(expr "$REPLY" : '.*\(Music .*\)')"

  # Check for Tremor decoder.
  [[ "$REPLY" =~ Loaded\ [0-9]+\ decoders:.*vorbis\(tremor\) ]] && \
     TREMOR=true

  # Extract file's full pathname.
  [[ "$REPLY" =~ "Playing item" ]] && \
     FILE="$(expr "$REPLY" : '.* item [0-9]*: \(.*\)')"

  # Ignore all non-MD5 lines.
  [[ "$REPLY" =~ "MD5" ]] || continue

  # Extract fields of interest.
  NAME="$(expr "$REPLY" : '.*MD5(\([^)]*\))')"
  set -- $(expr "$REPLY" : '.*MD5([^)]*) = \(.*\)')
  SUM=$1
  LEN=$2
  $TREMOR && [[ "$3" = "vorbis" ]] && DEC=tremor || DEC=$3
  FMT=$4
  CHANS=$5
  RATE=$6

  # Check that we have the full pathname and it's not a dangling symlink.
  [[ "$NAME" = "$(basename "$FILE")" ]] || die Filename mismatch
  [[ -L "$FILE" && ! -f "$FILE" ]] && continue

  # Get the independant MD5 sum and length of audio file.
  case $DEC in
  aac|ffmpeg|flac|mp3|sndfile|speex|vorbis)
      IGNORE_LEN=false
      IGNORE_SUM=false
      $DEC
      SUM2=$(expr "$SUM2" : '\([^ ]*\)')
      ;;
  modplug|musepack|sidplay2|timidity|tremor|wavpack)
      $IGNORE && continue
      [[ "${UNSUPPORTED[$DEC]}" ]] || {
        echo -e "*** Decoder not yet supported: $DEC\n" > /dev/stderr
        UNSUPPORTED[$DEC]="Y"
      }
      continue
      ;;
  *)  [[ "${UNKNOWN[$DEC]}" ]] || {
        echo -e "*** Unknown decoder: $DEC\n" > /dev/stderr
        UNKNOWN[$DEC]="Y"
      }
      continue
      ;;
  esac

  # Compare results.
  BADFMT=false
  $EXTRA && [[ "$NAME" == "sinewave-*" ]] && {
    set -- $(echo $NAME | tr '.-' ' ')
    FMT2=${2/24/32/}
    CHANS2=$3
    RATE2=$4
    [[ "$FMT" = "$FMT2" ]] || BADFMT=true
    [[ "$CHANS" = "$CHANS2" ]] || BADFMT=true
    [[ "$RATE" = "$RATE2" ]] || BADFMT=true
  }
  BADSUM=false; $IGNORE_SUM || [[ "$SUM" = "$SUM2" ]] || BADSUM=true
  BADLEN=false; $IGNORE_LEN || [[ "$LEN" = "$LEN2" ]] || BADLEN=true

  # Set exit code.
  $BADFMT || $BADSUM || $BADLEN && RC=2

  # Determine output requirements.
  $SILENT && continue
  $BADFMT || $BADSUM || $BADLEN || $VERBOSE || continue

  # Report result.
  [[ "$REVN" ]] && {
    echo "Test Results for $REVN"
    echo
    REVN=
  }
  echo "$NAME:"
  echo "    $SUM $LEN $DEC $FMT $CHANS $RATE"
  echo "    $SUM2 $LEN2"
  $BADFMT && echo "*** Format mismatch"
  $BADSUM && echo "*** MD5 sum mismatch"
  $BADLEN && echo "*** Length mismatch"
  echo

done < $LOG

$SILENT || {
  case "$RV" in
  1)
    echo "No mismatches found"
    ;;
  2)
    echo "NOTE: This tool is still being refined.  Do not accept mismatches"
    echo "      at face value; they may be due to factors such as sample size"
    echo "      differences.  But it does provide a reason to investigate"
    echo "      such mismatches further (and further refine this tool if false)."
    echo
    ;;
  esac
}

exit $RC