tags:

views:

318

answers:

4

I'm trying to write a customization in Lyris ListManager (10.2). The language is TCL, which I know very little about. We need to encode a value as base64 (or really, anything that obfuscates a querystring parameter), but I can't seem to figure out how. Is there a command native to TCL to do this?

+2  A: 

The existence of http://tcllib.sourceforge.net/doc/base64.html seems to indicate that there're no native functions. You could copy the source and add it to your modifications.

% base64::encode "Hello, world"
SGVsbG8sIHdvcmxk

% base64::encode [string repeat xyz 20]
eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6
eHl6eHl6eHl6
% base64::encode -wrapchar "" [string repeat xyz 20]
eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6

# NOTE: base64 encodes BINARY strings.
% set chemical [encoding convertto utf-8 "C\u2088H\u2081\u2080N\u2084O\u2082"]
% set encoded [base64::encode $chemical]
Q+KCiEjigoHigoBO4oKET+KCgg==
% set caffeine [encoding convertfrom utf-8 [base64::decode $encoded]]
Vinko Vrsalovic
So if I copy the base64.tcl file into that folder, is there a way to import it so I can make the call?
Chad
You can run the command "source base64.tcl" to load the file into the current namespace.
Bryan Oakley
when I try to use source base64.tcl I get the following *invalid command name "source" while executing "source "base64.tcl""*
Chad
If it doesn't recognise the source command - http://docs.activestate.com/activetcl/8.5/tcl/TclCmd/source.htm - then it's not standard Tcl. You will have to consult Lyris's documentation to find how they have modified it.
Colin Macleod
+1  A: 

If you have the ability to load Tcl libraries (packages/modules) into your environment, you can just use the Tcllib implementation. That's what Vinko Vrsalovic was showing the command from in his response.

% package require base64
2.4
% base64::encode bob
Ym9i
RHSeeger
+1  A: 

If it would be good enough to just encode in hexadecimal, you can use the binary command as follows:

% set query "Hello, world"
Hello, world
% binary scan $query H* hexquery
1
% puts $hexquery
48656c6c6f2c20776f726c64
Colin Macleod
+2  A: 

Following your problem to use the base64 package you can use these little procs to convert your data to hex and back. Requires Tcl > 8

proc BIN2HEX { text }   { binary scan $text H* result; return $result }
proc HEX2BIN { hex }    { return [binary format H* $hex] }
set hex [BIN2HEX $yourText]
set textAgain [HEX2BIN $hex]

If you really need base64 you can just copy/paste the entire base64 file from the tcllib distribution http://sourceforge.net/projects/tcllib/files/tcllib/1.11.1/ into your code (remove the "package provides" line)

Nir Levy
While the other answers are all good, this worked perfectly. I just had to copy/paste those methods, call them and I was done. Thanks... I wish Lyris support was as good as Stack Overflow
Chad