Re: BASH question/manual



Lew Pitcher wrote:
Herbert Kleebauer wrote:

I know that there are tools to decode base64. What I want is a way
to transfer a Linux binary over a 7 bit ASCII connection (for
example a news group) as a self extracting script which doesn't
require ANY tool (beside bash) on the target system to generate
the binary.

While I'm not going to try to discourage you from this goal, I do have
to ask how realistic it is. Consider that a "Linux binary" typically
contains only the core code of an application, and requires additional
binaries (in the form of .so libraries) before it can execute.

Real assembly programmer rely on nothing but the int 80 interface.
Yes I know, that makes the code dependent on the kernel version but
this is much better than to be dependent on the existence of some
libraries. The only problem is the lack of documentation, especially
when connecting to the X server by unix domain sockets (again
the documentation situation for Windows im much better than for
the open source Linux).

Consider
also that most Linux systems come with a core of utilities besides and
including bash that are available (and often required) for proper
system use. It seems highly unlikely that your target system will not
need components outside of those you are packaging in your script, and
will not have the common tools of the system.

NAME
shar - create shell archives

At least on my Linspire Linux there is no "shar".

But with the help of Stephane I was able to shorten the code (still
not as compact as I would like, but already better):

#!/bin/bash
name=dos2unix
echo -n>$name
x=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789123
for (( i=0; i<65; i++ )); do eval _$(( $i/62 ))${x:$i:1}=$(( $i&63 )); done
n=0; m=0
(while read -n 1 c; do
case $c in [A-Za-z0-9+/=])
if [ "$c" == "+" ]; then c=11; elif [ "$c" == "/" ]; then c=12;
elif [ "$c" == "=" ]; then c=13; else c=0$c; fi
e=$d; d=$c ; eval c=\$_$c; n=$(( $n*64+$c )); m=$(( $m+1 ))
if [ "$m" == "4" ]; then
echo -n -e \\$(( ($n>>22&7)*100+($n>>19&7)*10+($n>>16&7) ))>>$name
if [ "$e" != "13" ]; then
echo -n -e \\$(( ($n>>14&7)*100+($n>>11&7)*10+($n>>8&7) ))>>$name; fi
if [ "$d" != "13" ]; then
echo -n -e \\$(( ($n>>6&7) *100+($n>>3&7)*10+($n&7) ))>>$name; fi
n=0; m=0
fi
esac
done)<<"---";
f0VM RgEBAQAA AAAAAAAAAA IAAwABAAAAdIAEC DQAAAAAAAAAAAAAAD QAIAACAAAAAAAAAAEA
AAAAAAAAAI AECACABAj3AAAA 9wAAAAUAAAAA EAAAAQAAAPgAAA D4kAQI+J AECAAAAAAEAAAA
BgAAAAAQA ADoHgAAAD3/// //dAs8DXTw6EM AAADr6bsAAAAAuA EAAADNgGC7AAA AALn4kAQI
ugEAAAC4Aw AAAM2AhcB 4EGGQdAgP tgX4kAQIw4PI/8O Dy/+4AQAA AM2AYLsBAAAAufiQBAiI
AboBAAAAu AQAAADNgIP4A XUDYZDDg8v/uAEA AADNgAA=
---
chmod +x $name
#############################################################################


Here the source of the generated binary (convert DOS text files to Unix
text files <CR><LF> -> <LF>):

;===========================================================================

seg32
@=$08048000
code_offset=@@
code_addr:

;--------------------------- ELF header -----------------------------------

dc.l $464c457f,$00010101,0,0,$00030002,1,main,$34,0,0,$00200034,2,0
dc.l 1,code_offset,code_addr,code_addr,code_filez,code_memsz,5,4096
dc.l 1,data_offset,data_addr,data_addr,data_filez,data_memsz,6,4096

;--------------------------- code ------------------------------------------

main:
_10: bsr.l getc
cmp.l #-1,r0
beq.b _20
cmp.b #13,r0
beq.b _10
bsr.l putc
br.b _10

_20: move.l #0,r3 ; return code
move.l #1,r0 ; exit
trap #$80


getc: movem.l r0-r7,-(sp)
move.l #0,r3 ; stdin
move.l #buf,r2
move.l #1,r1 ; 1 byte
move.l #3,r0 ; read
trap #$80
tst.l r0,r0
bmi.b _10
movem.l (sp)+,r0-r7
beq.b _20
movu.bl buf,r0
rts.l
_20: orq.l #-1,r0
rts.l
_10: orq.l #-1,r3 ; return code
move.l #1,r0 ; exit
trap #$80


putc: movem.l r0-r7,-(sp)
move.l #1,r3 ; stdout
move.l #buf,r2
move.b r0,(r2)
move.l #1,r1 ; 1 byte
move.l #4,r0 ; write
trap #$80
cmpq.l #1,r0
bne.b _10
movem.l (sp)+,r0-r7
rts.l
_10: orq.l #-1,r3 ; return code
move.l #1,r0 ; exit
trap #$80

;--------------------------- constant data ---------------------------------


;---------------------------------------------------------------------------

code_filez=@@-code_offset
code_memsz= @-code_addr
even 4
@=(@+4095)/4096*4096+(@@\4096)
data_offset=@@
data_addr:

;--------------------------- initialized data ------------------------------

;var1: dc.l 1
;var2: dc.l 11

;--------------------------- uninitialized data ----------------------------

buf: blk.b 4

;---------------------------------------------------------------------------

data_filez=@@-data_offset
data_memsz= @-data_addr

;===========================================================================
.



Relevant Pages

  • Page fault traps in the linuxulator
    ... In 5.2-CURRENT I'm running some Linux code that needs ... to trap page faults. ... The trap handler has worked this way since trap.c revision 1.25 (soon to be ...
    (freebsd-hackers)
  • [SLE] Re: Web/Mail Software 9.1
    ... In a previous message, LeRoy DeVries wrote: ... Can anyone recommend a better mouse trap ... > for linux. ...
    (SuSE)
  • RE: Probably silly Q
    ... even execute scripts to process the incomming data), ... I have been using it for years on both Windows and Linux. ... If the firewall box is a linux box that you want to be receiving ... address of the specific box that you want the trap sent to. ...
    (Fedora)
  • Re: In the Last Hour
    ... falling into the trap of confusing Darwin with Linux again. ...
    (uk.comp.sys.mac)