File: base64_encode_atom.c

package info (click to toggle)
putty 0.83-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,216 kB
  • sloc: ansic: 148,476; python: 8,466; perl: 1,830; makefile: 128; sh: 117
file content (30 lines) | stat: -rw-r--r-- 704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Core routine to encode a single atomic base64 chunk.
 */

#include "defs.h"
#include "misc.h"

void base64_encode_atom(const unsigned char *data, int n, char *out)
{
    static const char base64_chars[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    unsigned word;

    word = data[0] << 16;
    if (n > 1)
        word |= data[1] << 8;
    if (n > 2)
        word |= data[2];
    out[0] = base64_chars[(word >> 18) & 0x3F];
    out[1] = base64_chars[(word >> 12) & 0x3F];
    if (n > 1)
        out[2] = base64_chars[(word >> 6) & 0x3F];
    else
        out[2] = '=';
    if (n > 2)
        out[3] = base64_chars[word & 0x3F];
    else
        out[3] = '=';
}