djbdnscurve6  38
djbdnscurve6
base32.c
Go to the documentation of this file.
1 #include "base32.h"
2 #include "byte.h"
3 
4 unsigned int base32_bytessize(unsigned int len)
5 {
6  len = (8 * len + 4) / 5;
7  return len + (len + 49) / 50;
8 }
9 
10 unsigned int base32_decode(char *out,const char *in,unsigned int len,int mode)
11 {
12  /*
13  * digits = '0123456789bcdfghjklmnpqrstuvwxyz'
14  * ','.join('%2d' % digits.find(chr(x).lower()) for x in xrange(256))
15  */
16  static const char val[256] = {
17  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
18  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
19  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
20  0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
21  -1,-1,10,11,12,-1,13,14,15,-1,16,17,18,19,20,-1,
22  21,22,23,24,25,26,27,28,29,30,31,-1,-1,-1,-1,-1,
23  -1,-1,10,11,12,-1,13,14,15,-1,16,17,18,19,20,-1,
24  21,22,23,24,25,26,27,28,29,30,31,-1,-1,-1,-1,-1,
25  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
26  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
27  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
28  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
29  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
30  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
31  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
32  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
33  };
34  unsigned int i;
35  unsigned int x;
36  unsigned int v;
37  unsigned int vbits;
38  char *out0 = out;
39 
40  v = vbits = 0;
41 
42  for (i = 0; i < len; ++i) {
43  x = (unsigned char) val[(unsigned char) in[i]];
44  if (x >= 32) return 0;
45  v |= x << vbits;
46  vbits += 5;
47  if (vbits >= 8) {
48  *out++ = v;
49  v >>= 8;
50  vbits -= 8;
51  }
52  }
53  if (mode) {
54  if (vbits) *out++ = v;
55  }
56  else if (vbits >= 5 || v)
57  return 0;
58  return out - out0;
59 }
60 
61 void base32_encodebytes(char *out,const char *in,unsigned int len)
62 {
63  unsigned int i;
64  unsigned int x;
65  unsigned int v;
66  unsigned int vbits;
67 
68  x = v = vbits = 0;
69 
70  for (i = 0; i < len; ++i) {
71  v |= ((unsigned int) (unsigned char) in[i]) << vbits;
72  vbits += 8;
73  do {
74  out[++x] = base32_digits[v & 31];
75  v >>= 5;
76  vbits -= 5;
77  if (x == 50) {
78  *out = x;
79  out += 1 + x;
80  x = 0;
81  }
82  } while (vbits >= 5);
83  }
84  if (vbits) out[++x] = base32_digits[v & 31];
85  if (x) *out = x;
86 }
87 
88 void base32_encodekey(char *out,const char *key)
89 {
90  unsigned int i;
91  unsigned int v;
92  unsigned int vbits;
93 
94  byte_copy(out,4,"\66x1a");
95  out += 4;
96  v = vbits = 0;
97 
98  for (i = 0; i < 32; ++i) {
99  v |= ((unsigned int) (unsigned char) key[i]) << vbits;
100  vbits += 8;
101  do {
102  *out++ = base32_digits[v & 31];
103  v >>= 5;
104  vbits -= 5;
105  } while (vbits >= 5);
106  }
107 }
uint16 len
Definition: axfrdns.c:302
unsigned int base32_bytessize(unsigned int len)
Definition: base32.c:4
void base32_encodebytes(char *out, const char *in, unsigned int len)
Definition: base32.c:61
unsigned int base32_decode(char *out, const char *in, unsigned int len, int mode)
Definition: base32.c:10
void base32_encodekey(char *out, const char *key)
Definition: base32.c:88
struct line * x
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
char mode
Definition: tinydns-edit.c:43