-
Notifications
You must be signed in to change notification settings - Fork 208
/
base64.prg
90 lines (78 loc) · 2.26 KB
/
base64.prg
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
/* TIP Base64 (and other) encoding
*
* This test writes data to standard output, and is
* compiled only under GTCGI. This allow to test the
* input/output file against other OS encoded/decoded data
*/
#require "hbtip"
#include "fileio.ch"
PROCEDURE Main( ... )
LOCAL oEncoder, cEncoder := "base64"
LOCAL lDecode := .F., lHelp := .F.
LOCAL cData, nLen, cBuffer
LOCAL hFileInput, nFileInput := hb_GetStdIn()
LOCAL hFileOutput, nFileOutput := hb_GetStdOut()
/* Parameter parsing */
FOR EACH cData IN hb_AParams()
SWITCH Lower( cData )
CASE "-h"
lHelp := .T.
EXIT
CASE "-d"
lDecode := .T.
EXIT
CASE "-q"
cEncoder := "quoted-printable"
EXIT
CASE "-u"
cEncoder := "url"
EXIT
OTHERWISE
IF hb_vfExists( cData ) .AND. nFileInput == hb_GetStdIn()
IF ( hFileInput := hb_vfOpen( cData, FO_READ ) ) != NIL
nFileInput := hb_vfHandle( hFileInput )
ENDIF
ELSEIF nFileOutput == hb_GetStdOut()
IF ( hFileOutput := hb_vfOpen( cData, FO_CREAT + FO_TRUNC + FO_WRITE ) ) != NIL
nFileOutput := hb_vfHandle( hFileOutput )
ENDIF
ELSE
? "Wrong parameter", cData
?
lHelp := .T.
ENDIF
ENDSWITCH
NEXT
/* Providing help */
IF lHelp
? "Usage:"
? "base64 [<] file-to-encode [>] encoded-file"
? "base64 -d [<] encoded-file [>] file-to-decode"
? "base64 -q [-d] to use quoted printable encoding/decoding"
? "base64 -u [-d] to use URL encoding/decoding."
? "input/output redirection is optional"
?
RETURN
ENDIF
/* Reading input stream */
cData := ""
cBuffer := Space( 1024 )
DO WHILE ( nLen := FRead( nFileInput, @cBuffer, hb_BLen( cBuffer ) ) ) > 0
cData += hb_BLeft( cBuffer, nLen )
ENDDO
IF hFileInput != NIL
hb_vfClose( hFileInput )
ENDIF
/* Encoding/decoding */
oEncoder := TIPEncoder():New( cEncoder )
IF lDecode
cData := oEncoder:Decode( cData )
ELSE
cData := oEncoder:Encode( cData )
ENDIF
/* Writing stream */
FWrite( nFileOutput, cData )
IF hFileOutput != NIL
hb_vfClose( hFileOutput )
ENDIF
RETURN