|
D* URL Escape an EBCDIC string. Returns an EBCDIC escaped string, but
* per the standard, the hex codes are actually the ASCII codes
D HTTPEncode pr 65535 varying
D iFrom 65535 varying options(*varsize) const
I've written the above as a service program, but it just seems like a
problem waiting to happen as soon as one wants to encode more than 64K
worth of data. In fact, since the output data needs to get larger
(generally speaking), the case where the iFrom field is close to full will
likely cause the result to likely overflow. In fact, the worst case is
that the output of the function must return three times the number of
bytes as the input buffer.
I've considered passing in a pointer and length, which I don't really
mind, but passing a return pointer has me a bit bothered. Only the
routine would know the correct return buffer length, and I'm concerned
that having the HTTPEncode routine allocate memory is just a really nice
way to leak a lot of memory, as I'm sure the callers of the routine will
forget to DEALLOCate the memory when they are through with it.
Is there a 'proper' solution to writing functions that must return an
arbitrarily large buffer?
Thanks!
Regards,
Rich
Here's the whole module, if that helps:
H
Nomain
H copyright('(c) 2003, Western Midrange Corp. All Rights
Reserved.')
* These characters require no
conversion
D NoConv s 1A DIM(72) PERRCD(36)
CTDATA
* This is the EBCDIC to ASCII table data from table
QTCPASC
D EbcAsc s 2A DIM(256) PERRCD(32)
CTDATA
* Handy for converting a character to its decimal
ordinal
* or converting a decimal to a
character
D ord
ds
D dec 1 2b
0
D char 2
2A
D* URL Escape an EBCDIC string. Returns an EBCDIC escaped string,
but
* per the standard, the hex codes are actually the ASCII
codes
D HTTPEncode pr 65535
varying
D iFrom 65535 varying options(*varsize)
const
P HTTPEncode b
export
D HTTPEncode pi 65535
varying
D iFrom 65535 varying options(*varsize)
const
D result s 65535
varying
D i s 9p
0
D n s 9p
0
D fc s
1
/free
for i=1 to
%len(iFrom);
fc =
%subst(iFrom:i:1);
n =
%lookup(fc:NoConv);
if n >
0;
result = result +
fc;
else;
if fc = '
';
result = result +
'+';
else;
// Use the hex code for the _ASCII_
character
dec = 0; // prep hex
conversion
char = fc; // converts hex char in char
to
// decimal value in
dec
// dec is used as array index for ascii
conv
result = result + '%' +
EbcAsc(dec+1);
endif;
endif;
endfor;
return
result;
/end-free
P HTTPEncode
e
**CTDATA
NoConv
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyz*@._-$!'()
**CTDATA
EbcAsc
000102031A091A7F1A1A1A0B0C0D0E0F101112131A1A081A18191A1A1C1D1E1F
1A1A1C1A1A0A171B1A1A1A1A1A0506071A1A161A1A1E1A041A1A1A1A14151A1A
20A6E180EB909FE2AB8B9B2E3C282B7C26A9AA9CDBA599E3A89E21242A293B5E
2D2FDFDC9ADDDE989DACBA2C255F3E3FD78894B0B1B2FCD6FB603A2340273D22
F861626364656667686996A4F3AFAEC58C6A6B6C6D6E6F7071729787CE93F1FE
C87E737475767778797AEFC0DA5BF2F9B5B6FDB7B8B9E6BBBCBD8DD9BF5DD8C4
7B414243444546474849CBCABEE8ECED7D4A4B4C4D4E4F505152A1ADF5F4A38F
5CE7535455565758595AA0858EE9E4D130313233343536373839B3F7F0FAA7FF
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.