string/append and base64

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

string/append and base64

Post by cormullion »

Not sure what I'm doing, but I don't understand why these two give different results:

Code: Select all

(println (append "AUTH PLAIN " (base64-enc (append "\000" "user.name" "\000" "password"))))
(println (append "AUTH PLAIN " (base64-enc (string "\000" "user.name" "\000" "password"))))

AUTH PLAIN AHVzZXIubmFtZQBwYXNzd29yZA==
AUTH PLAIN dXNlci5uYW1lcGFzc3dvcmQ=
I'm adapting SMTP.lsp to do authorization, but why should string be different? Is it a Unicode thing?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

'append' works on binary (non displayable ASCII). 'string' does not, it will take a \000 as a string terminator, like 'print' and 'println' which are based on 'string':

Code: Select all

> (string "\000")
""
> (append "\000")
"\000"
> 
Lutz

ps: see docs for 'append' and 'string'. Do we need a chapter/paragraph explaining this, perhaps in
http://newlisp.org/downloads/newlisp_ma ... l#type_ids ?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks, Lutz. I remember now. I was confusing myself partly by working in a file rather than directly in the terminal. I was trying to decode a string:

Code: Select all

(set 's (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA=="))
(println s)
- of course println returns the value correctly ("\000user.name\000password") but it doesn't display anything.

For some reason, the AUTH part of SMTP uses these zero terminators so I got myself well confused...

What's a good way of displaying the contents of strings like this in a log file?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Use plain 'replace' without the regular expressions option:

Code: Select all

(replace "\000" (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA==") " ")
=> " user.name password"
to change the "\000" to a space or some other character.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks! In fact. I'll replace it with "\\000" just so that I know what's happening...

Locked