* moved some code around

This commit is contained in:
sdomi 2024-05-02 02:52:42 +02:00
parent 1239a42509
commit bd86380275
4 changed files with 164 additions and 99 deletions

57
bin.sh Normal file
View file

@ -0,0 +1,57 @@
#!/bin/bash
# bin.sh - hot binary primitives in your area
# hex() {
# # loosely adapted from a comment by anonymouse under https://sdomi.pl/weblog/?id=15
# local LC_ALL= LC_CTYPE=C LC_COLLATE=C LANG= IFS="" c b h hs=
# while read -r -s -d "" -n 1 c; do
# printf -v b "%02x" "'$c"
# (( 0x$b > 127 )) && hs+="${b:2}" || hs+="${b}"
# done
#
# echo -n "$hs"
# }
#
# r() {
# # loosely adapted from a comment by anonymouse under https://sdomi.pl/weblog/?id=15
# local LC_ALL= LC_CTYPE=C LC_COLLATE=C LANG= IFS="" c b h o=1
# declare -g hs=
# while read -r -s -d "" -n 1 c; do
# printf -v b "%02x" "'$c"
# (( 0x$b > 127 )) && hs+="${b:2}" || hs+="${b}"
# (( $o >= $1 )) && break
# (( o++ ))
# done
#
# #echo -n "$hs"
# }
r() {
dd status=none bs=1 count=$1 | hex
}
rr() {
#read -n $(($1*2)) res
res=${data:$i:$(($1*2))}
((i=i+($1*2)))
}
hex() {
if [[ "$1" ]]; then
echo -n "$1" | xxd -p | tr -d '\n'
else
xxd -p | tr -d '\n'
fi
}
br() {
xxd -p -r
}
ulong() {
printf '%08x' "$1"
}
byte() {
printf '%02x' "$1"
}

View file

@ -8,5 +8,4 @@ declare -p d > /dev/stderr
pa_getsinks
#declare -p d > /dev/stderr
#sleep 15
sleep 15

133
func.sh
View file

@ -1,27 +1,7 @@
#!/bin/bash
_PA_SEQ_NUM=0
r() {
dd status=none bs=1 count=$1 | hex
}
rr() {
#read -n $(($1*2)) res
res=${data:$i:$(($1*2))}
((i=i+($1*2)))
}
hex() {
xxd -p -c999999
}
br() {
xxd -p -r
}
ulong() {
printf '%08x' "$1"
}
source bin.sh
source lib.sh
# as defined in /usr/include/pulse/def.h
pkt() {
@ -39,26 +19,49 @@ pkt() {
pkt_read() {
declare -g res
len="$(r 4)" # len
r 16 > /dev/null # idc about the header
len="$((0x$(r 4)))" # len
r 16 >/dev/null # idc about the header
res="$(r $((0x$len)))"
res="$(r $len)"
}
write_string() {
# t, then string, then nullbyte
echo -n "74$(hex <<< "$1")00" | sed -s 's/0a/65/g'
echo -n "74$(hex "$1")00" #| sed -s 's/0a/0a/g'
}
write_arbitrary() {
# x, then ulong length, then data
echo -n "78$(ulong $(($(wc -c <<< "$1")/2)))$1" | sed -s 's/0a/00/g' # TODO
echo -n "78$(ulong $(($(wc -c <<< "$1")/2)))$1" # TODO
}
write_long() {
echo -n "4c$(ulong "$1")"
}
write_samplespec() {
# a, then u8 format, then u8 channels, then u32 sample rate
local b
b="61" # 'a'
b+="$(byte 3)" # s16le
b+="01" # channels
b+="$(ulong 8000)" # samplerate
echo -n "$b"
}
write_channelmap() {
# m, then u8 channels, then *channel_pos...
local b
b="6d" # 'm'
b+="01" # channels
b+="00" # channelposition - mono
echo -n "$b"
}
# write_props(tag, payload, [tag, payload, ...])
write_props() {
local a
@ -78,12 +81,14 @@ parse() {
local data="$1"
local i=0
declare -g d=()
declare -g t=()
fi
while rr 1; do
# this is not a for.. for recursion purposes
[[ $i -ge ${#data} ]] && break
local type="$res"
t+=("$res")
case $type in
30) # 0 (BooleanFalse)
@ -100,9 +105,8 @@ parse() {
rr 4
d+=($res);;
4e) # N (NullString) TODO this the recursion check misaligns the pointer
;;
#[[ $1 == _ ]] && break;; # are we recursing? return
4e) # N (NullString) TODO
[[ $1 == _ ]] && break;; # are we recursing? return
50) # P (PropList)
parse _;; # recursion!
@ -127,6 +131,7 @@ parse() {
66) # f (FormatInfo) TODO
rr 1 # encoding
rr 1 # ????
parse _;;
6d) # m (ChannelMap) TODO
@ -153,7 +158,7 @@ parse() {
d+=("$res");;
*)
[[ "$type" != '' ]] && echo "wut? got type $type" > /dev/stderr
[[ "$type" != '' ]] && echo "wut? got type $type; ${t[-1]}:${d[-1]} ${t[-2]}:${d[-2]};" > /dev/stderr
return;;
esac
done
@ -162,69 +167,3 @@ parse() {
echo "Error: ${d[2]}" > /dev/stderr
fi
}
pa_auth() {
local c
local b
if [[ "$PULSE_COOKIE" != '' ]]; then
c="$(hex < "$PULSE_COOKIE")"
elif [[ -f ~/.config/pulse/cookie ]]; then
c="$(hex < ~/.config/pulse/cookie)"
elif [[ -f ~/.pulse-cookie ]]; then
c="$(hex < ~/.pulse-cookie)"
else # strict diet
:
fi
b="$(write_long "8")" # Auth
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="4c00000023" # 0000 - we don't support SHM/MEMFD; 0x23 - max version
b+="$(write_arbitrary "$c")"
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_setclientname() {
local b
#set -x
b="$(write_long "9")" # SetClientName
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="$(write_props "application.name" "$(hex <<< meowwwww)")"
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_getsinks() {
local b
b="$(write_long "22")" # GetSinkInfoList
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_createplaybackstream() {
local b
b="$(write_long "3")"
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="" # TODO
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}

70
lib.sh Normal file
View file

@ -0,0 +1,70 @@
#!/bin/bash
# lib.sh - stuff you might wanna call
pa_auth() {
local c
local b
if [[ "$PULSE_COOKIE" != '' ]]; then
c="$(hex < "$PULSE_COOKIE")"
elif [[ -f ~/.config/pulse/cookie ]]; then
c="$(hex < ~/.config/pulse/cookie)"
elif [[ -f ~/.pulse-cookie ]]; then
c="$(hex < ~/.pulse-cookie)"
else # strict diet
:
fi
b="$(write_long "8")" # Auth
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="4c0000000d" # 0000 - we don't support SHM/MEMFD; 0x0d - max version (used to be 0x23, but lower == easier)
b+="$(write_arbitrary "$c")"
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_setclientname() {
local b
#set -x
b="$(write_long "9")" # SetClientName
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="$(write_props "application.name" "$(hex meow)00" "meowmeow.meowmeow" "00")"
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_getsinks() {
local b
b="$(write_long "22")" # GetSinkInfoList
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}
pa_createplaybackstream() {
local b
b="$(write_long "3")"
b+="$(write_long "$_PA_SEQ_NUM")" # sequence number
b+="$(write_samplespec)" # TODO, hardcoded
b+="$(write_channelmap)" # ^
b+=""
pkt "$b"
pkt_read
parse "$res"
(( _PA_SEQ_NUM++ ))
}