tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
* SUMMARY: base64 encoder/decoder
@ 1996-08-14  5:28 e_rossi
  0 siblings, 0 replies; only message in thread
From: e_rossi @ 1996-08-14  5:28 UTC (permalink / raw)
  To: tru64-unix-managers

1. Burch Seymour	<bseymour@encore.com>
2. Anthony D'Atri	<aad@nwnet.net>
3. <radgovwa@wt.com.au>
4. <carpe@tus001.unitus.it>
5. Andres Henckens <henckens@luc.ac.be>
6. <fcosta@padova.infn.it>
7. Ollivier Robert	<ollivier.robert@eurocontrol.fr>

Thanks to all of above that gave me pointers about the base64 encoder/decoder.

In summary, a good package is named mpack and is located in
ftp://ftp.andrew.cmu.edu//pub/mpack. The filename is mpack-1.5-src.tar.gz.  It
works very good under Digital UNIX. There are executables too in the same
location for MS-DOS and OS/2.

Special thanks to Carlo <carpe@tus001.unitus.it> for his source code included
below.

Enjoy your self...

Enrique Rossi
erossi@sonda.cl
\f

DEBASE64.BAS
------------

100 DEFINT A-Z
110 CHARSET$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
120 DIM T(255),SEXTET(4)
130 FOR N=0 TO 255
140   T(N)=-1
150 NEXT N
160 FOR N=0 TO 63
170   I=ASC(MID$(CHARSET$,N+1,1))
180   T(I)=N
190 NEXT N
200 PRINT "INPUT? ";
210 LINE INPUT INPUT.FILE.NAME$
220 OPEN INPUT.FILE.NAME$ FOR INPUT AS 1
230 PRINT "OUTPUT? ";
240 LINE INPUT OUTPUT.FILE.NAME$
250 OPEN OUTPUT.FILE.NAME$ FOR OUTPUT AS 2
260 PRINT #2," ";
270 CLOSE 2
280 KILL OUTPUT.FILE.NAME$
290 OPEN "R",2,OUTPUT.FILE.NAME$,1
300 FIELD #2,1 AS BYTE$
310 INPUT.LINE$=""
320 INPUT.LINE.INDEX=81
330 INPUT.EOF=0
340 INPUT.EOF.PENDING=0
350 WHILE (INPUT.EOF = 0)
360   SEXTET.NUM=1
370   NUM.BITS=0
380   WHILE(SEXTET.NUM <= 4)
390     WHILE((INPUT.EOF = 0) AND (INPUT.LINE.INDEX > LEN(INPUT.LINE$)))
400       IF INPUT.EOF.PENDING = 0 THEN 430
410         INPUT.EOF=-1
420       GOTO 460
430         LINE INPUT #1,INPUT.LINE$
440         INPUT.LINE.INDEX=1
450         IF EOF(1) THEN INPUT.EOF.PENDING=-1
460     WEND
470     IF INPUT.EOF = 0 THEN 510
480       SEXTET(SEXTET.NUM)=0
490       SEXTET.NUM=SEXTET.NUM+1
500     GOTO 580
510       I=ASC(MID$(INPUT.LINE$,INPUT.LINE.INDEX,1))
520       INPUT.LINE.INDEX=INPUT.LINE.INDEX+1
530       T1=T(I)
540       IF T1 < 0 THEN 580
550         SEXTET(SEXTET.NUM)=T1
560         NUM.BITS=NUM.BITS+6
570         SEXTET.NUM=SEXTET.NUM+1
580   WEND
590   IF NUM.BITS < 8 THEN 660
600     T1=SEXTET(1)
610     T2=SEXTET(2)
620     TEM$=CHR$(4*T1+(T2\16))
630     LSET BYTE$=TEM$
640     PUT #2
650     NUM.BITS=NUM.BITS-8
660   IF NUM.BITS < 8 THEN 720
670     T1=SEXTET(3)
680     TEM$=CHR$(16*(T2 MOD 16)+(T1\4))
690     LSET BYTE$=TEM$
700     PUT #2
710     NUM.BITS=NUM.BITS-8
720   IF NUM.BITS < 8 THEN 770
730     T2=SEXTET(4)
740     TEM$=CHR$(64*(T1 MOD 4)+T2)
750     LSET BYTE$=TEM$
760     PUT #2
770 WEND
780 CLOSE 2
790 CLOSE 1
800 END
\f

DEBASE64.CMD
------------

/* Decodes a Base64 file.                            */
/*                                                   */
/* Written by James L. Dean                          */
char_set='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
DO n=0 to 127
  t.n=-1
END
DO n=0 to 63
  i=C2D(SUBSTR(char_set,n+1,1))
  t.i=n
END
CALL CHAROUT,'Input? '
input_file_name=LINEIN()
CALL CHAROUT,'Output? '
output_file_name=LINEIN()
SAY 'Writing "'output_file_name'".'
CALL RxFuncAdd 'SysFileDelete','RexxUtil','SysFileDelete'
i=SysFileDelete(output_file_name)
input_line=''
input_line_index=81
input_eof=0
DO WHILE (input_eof = 0)
  sextet_num=1
  num_bits=0
  DO WHILE(sextet_num <= 4)
    DO WHILE((input_eof = 0) & (input_line_index > LENGTH(input_line)))
      IF LINES(input_file_name) = 0 THEN
        input_eof=-1
      ELSE
        DO
          input_line=LINEIN(input_file_name)
          input_line_index=1
        END
    END
    IF input_eof = 0 THEN
      DO
        i=C2D(SUBSTR(input_line,input_line_index,1))
        input_line_index=input_line_index+1
        t1=t.i
        IF t1 >= 0 THEN
          DO
            sextet.sextet_num=t1
            num_bits=num_bits+6
            sextet_num=sextet_num+1
          END
      END
    ELSE
      DO
        sextet.sextet_num=0
        sextet_num=sextet_num+1
      END
  END
  IF num_bits >= 8 THEN
    DO
      t1=sextet.1
      t2=sextet.2
      CALL CHAROUT output_file_name,D2C(4*t1+t2%16)
      num_bits=num_bits-8
    END
  IF num_bits >= 8 THEN
    DO
      t1=sextet.3
      CALL CHAROUT output_file_name,D2C(16*(t2//16)+(t1%4))
      num_bits=num_bits-8
    END
  IF num_bits >= 8 THEN
    DO
      t2=sextet.4
      CALL CHAROUT output_file_name,D2C(64*(t1//4)+t2)
    END
END
\f

ENBASE64.CMD
------------

/* Encodes a file in Base64.                         */
/*                                                   */
/* Written by James L. Dean                          */
char_set='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
CALL CHAROUT,'Input? '
input_file_name=LINEIN()
CALL CHAROUT,'Output? '
output_file_name=LINEIN()
SAY 'Writing "'output_file_name'".'
CALL RxFuncAdd 'SysFileDelete','RexxUtil','SysFileDelete'
i=SysFileDelete(output_file_name)
input_eof=0
col_num=1
DO WHILE (input_eof = 0)
  num_octets=0
  triple=0
  DO octet_num=1 TO 3
    IF input_eof = 0 THEN
      DO
        octet=CHARIN(input_file_name)
        IF LENGTH(octet) = 0 THEN
          input_eof=-1
      END
    IF input_eof = 0 THEN
      DO
        triple=256*triple+C2D(octet)
        num_octets=num_octets+1
      END
    ELSE
      triple=256*triple
  END
  num_sextets=(8*num_octets)%6
  IF 6*num_sextets < 8*num_octets THEN
    num_sextets=num_sextets+1
  IF num_sextets > 0 THEN
    DO
      sextet_num=1
      DO WHILE(sextet_num <= 4)
        quotient=triple%64
        stack.sextet_num=triple-64*quotient
        sextet_num=sextet_num+1
        triple=quotient
      END
      DO WHILE(num_sextets >= 1)
        sextet_num=sextet_num-1
        rc=CHAROUT(output_file_name,SUBSTR(char_set,1+stack.sextet_num,1))
        col_num=col_num+1
        IF col_num > 76 THEN
          DO
            rc=CHAROUT(output_file_name,D2C(13))
            rc=CHAROUT(output_file_name,D2C(10))
            col_num=1
          END
        num_sextets=num_sextets-1
      END
      DO WHILE(sextet_num > 1)
        rc=CHAROUT(output_file_name,'=')
        col_num=col_num+1
        IF col_num > 76 THEN
          DO
            rc=CHAROUT(output_file_name,D2C(13))
            rc=CHAROUT(output_file_name,D2C(10))
            col_num=1
          END
        sextet_num=sextet_num-1
      END
    END
END
IF col_num > 1 THEN
  DO
    rc=CHAROUT(output_file_name,D2C(13))
    rc=CHAROUT(output_file_name,D2C(10))
  END

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1996-08-14  5:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-08-14  5:28 SUMMARY: base64 encoder/decoder e_rossi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox