File: encode_part.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 15,824; perl: 674; sh: 63; makefile: 29
file content (26 lines) | stat: -rw-r--r-- 828 bytes parent folder | download | duplicates (8)
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
#include "base64.h"

void base64_encode_whole(const unsigned char bin[3], char encoded[4])
{
  encoded[0] = base64_bin2asc[bin[0] >> 2];
  encoded[1] = base64_bin2asc[(bin[0] << 4 | bin[1] >> 4) & 0x3f];
  encoded[2] = base64_bin2asc[(bin[1] << 2 | bin[2] >> 6) & 0x3f];
  encoded[3] = base64_bin2asc[bin[2] & 0x3f];
}

void base64_encode_part(const unsigned char bin[3], unsigned len,
			char encoded[4])
{
  encoded[0] = base64_bin2asc[bin[0] >> 2];
  /* len must be 1 or 2, as 0 produces nothing, and 3 would be handled
   * by _whole */
  if (len == 1) {
    encoded[1] = base64_bin2asc[(bin[0] << 4) & 0x3f];
    encoded[2] = encoded[3] = BASE64_PAD;
  }
  else {
    encoded[1] = base64_bin2asc[(bin[0] << 4 | bin[1] >> 4) & 0x3f];
    encoded[2] = base64_bin2asc[(bin[1] << 2) & 0x3f];
    encoded[3] = BASE64_PAD;
  }
}