1 &ANALYZE-SUSPEND _VERSION-NUMBER UIB_v9r12
3 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _DEFINITIONS Method-Library
4 /*------------------------------------------------------------------------
15 ----------------------------------------------------------------------*/
16 /* This .W file was created with the Progress AppBuilder.
*/
17 /*----------------------------------------------------------------------*/
20 DEF STREAM base64stream.
21 DEF STREAM incomingfile.
23 /* _UIB-CODE-BLOCK-END
*/
27 &ANALYZE-SUSPEND _UIB-PREPROCESSOR-BLOCK
29 /* ******************** Preprocessor Definitions
******************** */
33 /* _UIB-PREPROCESSOR-BLOCK-END
*/
37 /* ************************ Function Prototypes
********************** */
39 &IF DEFINED(EXCLUDE-base64-encode) = 0 &THEN
41 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode Method-Library
42 FUNCTION base64-encode
RETURNS CHARACTER
44 input-stream
AS MEMPTR,
45 is-a-string
AS LOGICAL,
49 /* _UIB-CODE-BLOCK-END
*/
54 &IF DEFINED(EXCLUDE-base64-encode-file) = 0 &THEN
56 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode-file Method-Library
57 FUNCTION base64-encode-file
RETURNS CHARACTER
58 ( file-param
AS CHARACTER ) FORWARD.
60 /* _UIB-CODE-BLOCK-END
*/
65 &IF DEFINED(EXCLUDE-base64-encode-string) = 0 &THEN
67 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD base64-encode-string Method-Library
68 FUNCTION base64-encode-string
RETURNS CHARACTER
69 ( input-string
AS CHARACTER ) FORWARD.
71 /* _UIB-CODE-BLOCK-END
*/
77 /* *********************** Procedure Settings
************************ */
79 &ANALYZE-SUSPEND _PROCEDURE-SETTINGS
80 /* Settings for
THIS-PROCEDURE
84 Add Fields to
: Neither
85 Other Settings
: INCLUDE-ONLY
87 &ANALYZE-RESUME _END-PROCEDURE-SETTINGS
89 /* ************************* Create Window
************************** */
91 &ANALYZE-SUSPEND _CREATE-WINDOW
92 /* DESIGN Window definition
(used by the UIB
)
93 CREATE WINDOW Method-Library
ASSIGN
96 /* END WINDOW DEFINITION
*/
99 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _XFTR "MethodLibraryCues" Method-Library _INLINE
100 /* Actions
: adecomm
/_so-cue.w ? adecomm
/_so-cued.p ? adecomm
/_so-cuew.p
*/
101 /* Method Library
,uib
,70080
102 Destroy on next read
*/
103 /* _UIB-CODE-BLOCK-END
*/
107 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB Method-Library
108 /* ************************* Included-Libraries
*********************** */
110 /* _UIB-CODE-BLOCK-END
*/
117 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Method-Library
120 /* *************************** Main Block
*************************** */
122 /* _UIB-CODE-BLOCK-END
*/
126 /* ************************ Function Implementations
***************** */
128 &IF DEFINED(EXCLUDE-base64-encode) = 0 &THEN
130 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode Method-Library
131 FUNCTION base64-encode
RETURNS CHARACTER
133 input-stream
AS MEMPTR,
134 is-a-string
AS LOGICAL,
137 /*------------------------------------------------------------------------------
138 Purpose
: Encode a mem pointer binary stream into a base64 encoded string
140 Notes
: The following description of the base64 encoding process..
142 * Divide the input bytes stream into blocks of
3 bytes.
143 * Divide the
24 bits of a
3-byte block into
4 groups of
6 bits.
144 * Map each group of
6 bits to
1 printable character
, based on the
6-bit value.
145 * If the last
3-byte block has only
1 byte of input data
, pad
2 bytes of zero
146 (\x0000
). After encoding it as a normal block
, override the last
2 characters
147 with
2 equal signs
(==), so the decoding process knows
2 bytes of zero were
149 * If the last
3-byte block has only
2 bytes of input data
, pad
1 byte of zero
150 (\x00
). After encoding it as a normal block
, override the last
1 character
151 with
1 equal signs
(=), so the decoding process knows
1 byte of zero was
153 * Carriage return
(\r
) and new line
(\n
) are inserted into the output character
154 stream. They will be ignored by the decoding process.
156 ------------------------------------------------------------------------------*/
157 DEF VAR Codes64
AS CHARACTER NO-UNDO.
158 DEF VAR output-string
AS CHARACTER NO-UNDO.
159 DEF VAR v-count
AS INTEGER NO-UNDO.
160 DEF VAR v-index
AS INTEGER NO-UNDO.
161 DEF VAR grand-count
AS INTEGER INIT 0 NO-UNDO.
163 DEF VAR stream-length
AS INTEGER NO-UNDO.
165 DEF VAR first-int
AS INTEGER NO-UNDO.
166 DEF VAR second-int
AS INTEGER NO-UNDO.
167 DEF VAR third-int
AS INTEGER NO-UNDO.
168 DEF VAR last-block
AS LOGICAL INIT NO NO-UNDO.
170 DEF VAR integer-array
AS INTEGER EXTENT 4 NO-UNDO.
173 DEF VAR temp-file-name
AS CHARACTER NO-UNDO.
175 /* Encoding string
*/
176 Codes64
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
+/'.
178 /* If the output is set to go to a file
*/
180 temp-file-name
= SESSION:TEMP-DIRECTORY + 'BASE64'
+
181 STRING( TODAY, "99999999" ) +
183 STRING( RANDOM( 1, 200 ) ) + '.tmp'.
184 OUTPUT STREAM base64stream
TO VALUE( temp-file-name
).
187 /* If this is a
CHAR stream
, then ignore the last byte as it will be a
NULL */
188 stream-length
= GET-SIZE( input-stream
).
190 stream-length
= stream-length
- 1.
192 DO v-count
= 1 TO stream-length
BY 3:
194 IF v-count
+ 3 >= stream-length
THEN
197 integer-array
[1] = 0.
198 integer-array
[2] = 0.
199 integer-array
[3] = 0.
200 integer-array
[4] = 0.
202 /* Fetch the next three chars
*/
203 first-int
= GET-BYTE( input-stream
, v-count
).
204 second-int
= GET-BYTE( input-stream
, v-count
+ 1 ).
205 third-int
= GET-BYTE( input-stream
, v-count
+ 2 ).
207 /* Pack the three
8 bit integers into
4 "6 bit" integers
*/
208 PUT-BITS( integer-array
[1], 1, 6 ) = GET-BITS( first-int
, 3, 6 ).
209 PUT-BITS( integer-array
[2], 5, 2 ) = GET-BITS( first-int
, 1, 2 ).
210 PUT-BITS( integer-array
[2], 1, 4 ) = GET-BITS( second-int
, 5, 4 ).
211 PUT-BITS( integer-array
[3], 3, 4 ) = GET-BITS( second-int
, 1, 4 ).
212 PUT-BITS( integer-array
[3], 1, 2 ) = GET-BITS( third-int
, 7, 2 ).
213 PUT-BITS( integer-array
[4], 1, 6 ) = GET-BITS( third-int
, 1, 6 ).
216 /* Wrap the string every
76 characters
*/
217 IF grand-count
<> 0 AND grand-count
MODULO 72 = 0 THEN
218 output-string
= output-string
+ CHR(13) + CHR(10).
220 /* Check if the last two characters exist
*/
221 IF v-index
= 3 OR v-index
= 4 THEN DO:
222 IF last-block
AND integer-array
[v-index
] = 0 THEN
223 output-string
= output-string
+ '
='.
225 output-string
= output-string
+ SUBSTRING( Codes64
, integer-array
[v-index
] + 1, 1 ).
228 output-string
= output-string
+ SUBSTRING( Codes64
, integer-array
[v-index
] + 1, 1 ).
230 /* Count the chars for use in the
MODULO */
231 grand-count
= grand-count
+ 1.
234 /* If this data is going to a file then write the data and clear the string buffer
*/
236 PUT STREAM base64stream
UNFORMATTED output-string.
241 /* Return the temp-file name or the string
*/
243 PUT STREAM base64stream
UNFORMATTED CHR(10).
244 OUTPUT STREAM base64stream
CLOSE.
245 RETURN temp-file-name.
248 RETURN output-string.
252 /* _UIB-CODE-BLOCK-END
*/
257 &IF DEFINED(EXCLUDE-base64-encode-file) = 0 &THEN
259 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode-file Method-Library
260 FUNCTION base64-encode-file
RETURNS CHARACTER
261 ( file-param
AS CHARACTER ) :
262 /*------------------------------------------------------------------------------
263 Purpose
: Base64 encode a file
265 ------------------------------------------------------------------------------*/
266 DEF VAR fileptr
AS MEMPTR.
267 DEF VAR encoded-file-name
AS CHARACTER NO-UNDO.
269 FILE-INFO:FILE-NAME = file-param.
270 IF FILE-INFO:FILE-MOD-DATE = ?
THEN DO:
274 /* Slurp the file in
*/
275 SET-SIZE( fileptr
) = FILE-INFO:FILE-SIZE.
276 INPUT STREAM incomingfile
FROM VALUE( file-param
) BINARY.
277 IMPORT STREAM incomingfile fileptr.
278 INPUT STREAM incomingfile
CLOSE.
281 Encode the file in base64.
NO because not a string
, YES because into a temp file.
282 The function returns the temp file location of the encoded data
284 encoded-file-name
= base64-encode
( fileptr
, NO, YES ).
286 RETURN encoded-file-name.
290 /* _UIB-CODE-BLOCK-END
*/
295 &IF DEFINED(EXCLUDE-base64-encode-string) = 0 &THEN
297 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION base64-encode-string Method-Library
298 FUNCTION base64-encode-string
RETURNS CHARACTER
299 ( input-string
AS CHARACTER ) :
300 /*------------------------------------------------------------------------------
301 Purpose
: Base64 encode an arbritrary string
303 ------------------------------------------------------------------------------*/
304 DEF VAR string-pointer
AS MEMPTR.
305 SET-SIZE( string-pointer
) = LENGTH( input-string
) + 1.
307 PUT-STRING( string-pointer
, 1 ) = input-string.
310 YES because this function should ignore the terminating
NULL, and
NO
311 because the output should not go to a file.
313 RETURN base64-encode
( string-pointer
, YES, NO ).
317 /* _UIB-CODE-BLOCK-END
*/