191
* Base64-encode a block of data, stick "?OTR:" and "." around it, and
192
* return the result, or NULL in the event of a memory error. The
193
* caller must free() the return value.
195
char *otrl_base64_otr_encode(const unsigned char *buf, size_t buflen)
200
/* Make the base64-encoding. */
201
base64len = ((buflen + 2) / 3) * 4;
202
base64buf = malloc(5 + base64len + 1 + 1);
203
if (base64buf == NULL) {
206
memmove(base64buf, "?OTR:", 5);
207
otrl_base64_encode(base64buf+5, buf, buflen);
208
base64buf[5 + base64len] = '.';
209
base64buf[5 + base64len + 1] = '\0';
215
* Base64-decode the portion of the given message between "?OTR:" and
216
* ".". Set *bufp to the decoded data, and set *lenp to its length.
217
* The caller must free() the result. Return 0 on success, -1 on a
218
* memory error, or -2 on invalid input.
220
int otrl_base64_otr_decode(const char *msg, unsigned char **bufp,
223
char *otrtag, *endtag;
224
size_t msglen, rawlen;
225
unsigned char *rawmsg;
227
otrtag = strstr(msg, "?OTR:");
231
endtag = strchr(otrtag, '.');
233
msglen = endtag-otrtag;
238
/* Base64-decode the message */
239
rawlen = ((msglen-5) / 4) * 3; /* maximum possible */
240
rawmsg = malloc(rawlen);
241
if (!rawmsg && rawlen > 0) {
244
rawlen = otrl_base64_decode(rawmsg, otrtag+5, msglen-5); /* actual size */