~ubuntu-branches/ubuntu/hardy/vm/hardy

« back to all changes in this revision

Viewing changes to base64-encode.c

  • Committer: Bazaar Package Importer
  • Author(s): Manoj Srivastava
  • Date: 2002-03-06 00:46:29 UTC
  • Revision ID: james.westby@ubuntu.com-20020306004629-lnksqjffsnoc4tog
Tags: upstream-7.03
ImportĀ upstreamĀ versionĀ 7.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* public domain */
 
2
 
 
3
/*
 
4
 * arbitrary data on stdin -> BASE64 data on stdout
 
5
 *
 
6
 * UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).
 
7
 */
 
8
 
 
9
#include <stdio.h>
 
10
 
 
11
#ifdef _WIN32
 
12
#ifndef WIN32
 
13
#define WIN32
 
14
#endif
 
15
#endif
 
16
 
 
17
#ifdef WIN32
 
18
#include <io.h>
 
19
#include <fcntl.h>
 
20
#endif
 
21
 
 
22
unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
23
 
 
24
int
 
25
main()
 
26
{
 
27
    int cols, bits, c, char_count;
 
28
 
 
29
#ifdef WIN32
 
30
    _setmode( _fileno(stdin), _O_BINARY);
 
31
#endif
 
32
 
 
33
    char_count = 0;
 
34
    bits = 0;
 
35
    cols = 0;
 
36
    while ((c = getchar()) != EOF) {
 
37
        if (c > 255) {
 
38
            fprintf(stderr, "encountered char > 255 (decimal %d)", c);
 
39
            exit(1);
 
40
        }
 
41
        bits += c;
 
42
        char_count++;
 
43
        if (char_count == 3) {
 
44
            putchar(alphabet[bits >> 18]);
 
45
            putchar(alphabet[(bits >> 12) & 0x3f]);
 
46
            putchar(alphabet[(bits >> 6) & 0x3f]);
 
47
            putchar(alphabet[bits & 0x3f]);
 
48
            cols += 4;
 
49
            if (cols == 72) {
 
50
                putchar('\n');
 
51
                cols = 0;
 
52
            }
 
53
            bits = 0;
 
54
            char_count = 0;
 
55
        } else {
 
56
            bits <<= 8;
 
57
        }
 
58
    }
 
59
    if (char_count != 0) {
 
60
        bits <<= 16 - (8 * char_count);
 
61
        putchar(alphabet[bits >> 18]);
 
62
        putchar(alphabet[(bits >> 12) & 0x3f]);
 
63
        if (char_count == 1) {
 
64
            putchar('=');
 
65
            putchar('=');
 
66
        } else {
 
67
            putchar(alphabet[(bits >> 6) & 0x3f]);
 
68
            putchar('=');
 
69
        }
 
70
        if (cols > 0)
 
71
          putchar('\n');
 
72
    }
 
73
 
 
74
    exit(0);
 
75
}