~ubuntu-branches/ubuntu/oneiric/libotr/oneiric-security

« back to all changes in this revision

Viewing changes to src/b64.c

  • Committer: Bazaar Package Importer
  • Author(s): Thibaut VARENE
  • Date: 2006-01-02 19:52:18 UTC
  • mfrom: (2.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060102195218-wb8803196y9mycx6
Tags: 3.0.0-2
Fix typo: "malformed messahes" (Closes: #345400)

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 
57
57
/* system headers */
58
58
#include <stdlib.h>
 
59
#include <string.h>
59
60
 
60
61
/* libotr headers */
61
62
#include "b64.h"
185
186
 
186
187
    return datalen;
187
188
}
 
189
 
 
190
/*
 
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.
 
194
 */
 
195
char *otrl_base64_otr_encode(const unsigned char *buf, size_t buflen)
 
196
{
 
197
    char *base64buf;
 
198
    size_t base64len;
 
199
 
 
200
    /* Make the base64-encoding. */
 
201
    base64len = ((buflen + 2) / 3) * 4;
 
202
    base64buf = malloc(5 + base64len + 1 + 1);
 
203
    if (base64buf == NULL) {
 
204
        return NULL;
 
205
    }
 
206
    memmove(base64buf, "?OTR:", 5);
 
207
    otrl_base64_encode(base64buf+5, buf, buflen);
 
208
    base64buf[5 + base64len] = '.';
 
209
    base64buf[5 + base64len + 1] = '\0';
 
210
 
 
211
    return base64buf;
 
212
}
 
213
 
 
214
/*
 
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.
 
219
 */
 
220
int otrl_base64_otr_decode(const char *msg, unsigned char **bufp,
 
221
        size_t *lenp)
 
222
{
 
223
    char *otrtag, *endtag;
 
224
    size_t msglen, rawlen;
 
225
    unsigned char *rawmsg;
 
226
 
 
227
    otrtag = strstr(msg, "?OTR:");
 
228
    if (!otrtag) {
 
229
        return -2;
 
230
    }
 
231
    endtag = strchr(otrtag, '.');
 
232
    if (endtag) {
 
233
        msglen = endtag-otrtag;
 
234
    } else {
 
235
        return -2;
 
236
    }
 
237
 
 
238
    /* Base64-decode the message */
 
239
    rawlen = ((msglen-5) / 4) * 3;   /* maximum possible */
 
240
    rawmsg = malloc(rawlen);
 
241
    if (!rawmsg && rawlen > 0) {
 
242
        return -1;
 
243
    }
 
244
    rawlen = otrl_base64_decode(rawmsg, otrtag+5, msglen-5);  /* actual size */
 
245
 
 
246
    *bufp = rawmsg;
 
247
    *lenp = rawlen;
 
248
 
 
249
    return 0;
 
250
}