1 
/* 

2 
* Copyright (c) 20052007, OmniTI Computer Consulting, Inc. 

3 
* All rights reserved. 

4 
* 

5 
* Redistribution and use in source and binary forms, with or without 

6 
* modification, are permitted provided that the following conditions are 

7 
* met: 

8 
* 

9 
* * Redistributions of source code must retain the above copyright 

10 
* notice, this list of conditions and the following disclaimer. 

11 
* * Redistributions in binary form must reproduce the above 

12 
* copyright notice, this list of conditions and the following 

13 
* disclaimer in the documentation and/or other materials provided 

14 
* with the distribution. 

15 
* * Neither the name OmniTI Computer Consulting, Inc. nor the names 

16 
* of its contributors may be used to endorse or promote products 

17 
* derived from this software without specific prior written 

18 
* permission. 

19 
* 

20 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 

21 
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 

22 
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 

23 
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 

24 
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 

25 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 

26 
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 

27 
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 

28 
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 

29 
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 

30 
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

31 
*/ 

32 


33 
#include "noit_config.h" 

34 
#include "noit_b64.h" 

35 
#include <ctype.h> 

36 


37 
static const char __b32[] = { 

38 
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 

39 
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 

40 
'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', 0x00 }; 

41 


42 
static const unsigned char __ub32[] = { 

43 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

44 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

45 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

46 
0xfe, 0xfe, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0xfe, 0xfe, 

47 
0xfe, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 

48 
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

49 
0xfe, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 

50 
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

51 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

52 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

53 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

54 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

55 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

56 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

57 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 

58 
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe }; 

59 


60 
int 

61 
noit_b32_decode(const char *src, size_t src_len, 

62 
unsigned char *dest, size_t dest_len) { 

63 
const unsigned char *cp = (unsigned char *)src; 

64 
unsigned char *dcp = dest; 

65 
unsigned char ch, in[8] = { 0 }, out[5]; 

66 
int ib = 0, ob = 3, needed = ((src_len / 8) * 5); 

67 


68 
if(dest_len < needed) return 0; 

69 
while(cp <= ((unsigned char *)src+src_len)) { 

70 
if(isspace((int)*cp)) { cp++; continue; } 

71 
if(*cp == '') { cp++; continue; } 

72 
ch = __ub32[*cp]; 

73 
if(ch == 0xfe) break; 

74 
cp++; 

75 
if(ch == 0xff) { 

76 
if(ib == 0) break; 

77 
if(ib == 1  ib == 2) ob = 1; 

78 
else if(ib == 3  ib == 4) ob = 2; 

79 
else if(ib == 4  ib == 5) ob = 3; 

80 
else ob = 4; 

81 
ib = 7; 

82 
} 

83 
in[ib++] = ch; 

84 
if(ib == 8) { 

85 
out[0] = (in[0] << 3)  (in[1] >> 2); 

86 
out[1] = ((in[1] & 0x3) << 6)  (in[2] << 1)  (in[3] >> 4); 

87 
out[2] = ((in[3] & 0xf) << 4)  (in[4] >> 1); 

88 
out[3] = ((in[4] & 0x1) << 7)  (in[5] << 2)  (in[6] >> 3); 

89 
out[4] = ((in[6] & 0x7) << 5)  in[7]; 

90 
for(ib = 0; ib < ob; ib++) 

91 
*dcp++ = out[ib]; 

92 
ib = 0; 

93 
memset(in, 0, sizeof(in)); 

94 
} 

95 
} 

96 
return dcp  (unsigned char *)dest; 

97 
} 

98 


99 
int 

100 
noit_b32_encode(const unsigned char *src, size_t src_len, 

101 
char *dest, size_t dest_len) { 

102 
const unsigned char *bptr = src; 

103 
char *eptr = dest; 

104 
int len = src_len; 

105 
int i, n = (((src_len + 4) / 5) * 8); 

106 


107 
if(dest_len < n) return 0; 

108 


109 
while(len > 4) { 

110 
*eptr++ = __b32[bptr[0] >> 3]; /* [ xxxxx000 00000000 00000000 00000000 00000000 ] */ 

111 
*eptr++ = __b32[((bptr[0] & 0x07) << 2) + (bptr[1] >> 6)]; /* [ 00000xxx xx000000 00000000 00000000 00000000 ] */ 

112 
*eptr++ = __b32[(bptr[1] & 0x3e) >> 1]; /* [ 00000000 00xxxxx0 00000000 00000000 00000000 ] */ 

113 
*eptr++ = __b32[((bptr[1] & 0x1) << 4) + (bptr[2] >> 4)]; /* [ 00000000 0000000x xxxx0000 00000000 00000000 ] */ 

114 
*eptr++ = __b32[((bptr[2] & 0xf) << 1) + (bptr[3] >> 7)]; /* [ 00000000 00000000 0000xxxx x0000000 00000000 ] */ 

115 
*eptr++ = __b32[(bptr[3] & 0x7c) >> 2]; /* [ 00000000 00000000 00000000 0xxxxx00 00000000 ] */ 

116 
*eptr++ = __b32[((bptr[3] & 0x3) << 3) + (bptr[4] >> 5)]; /* [ 00000000 00000000 00000000 000000xx xxx00000 ] */ 

117 
*eptr++ = __b32[bptr[4] & 0x1f]; /* [ 00000000 00000000 00000000 00000000 000xxxxx ] */ 

118 
bptr += 5; 

119 
len = 5; 

120 
} 

121 
if(len != 0) { 

122 
*eptr++ = __b32[bptr[0] >> 3]; 

123 
if(len == 1) { 

124 
*eptr++ = __b32[(bptr[0] & 0x07) << 2]; 

125 
for(i=0;i<5;i++) *eptr++ = '='; 

126 
} 

127 
else { 

128 
*eptr++ = __b32[((bptr[0] & 0x07) << 2) + (bptr[1] >> 6)]; 

129 
*eptr++ = __b32[(bptr[1] & 0x3e) >> 1]; 

130 
if(len == 2) { 

131 
*eptr++ = __b32[(bptr[1] & 0x1) << 4]; 

132 
for(i=0;i<3;i++) *eptr++ = '='; 

133 
} 

134 
else { 

135 
*eptr++ = __b32[((bptr[1] & 0x1) << 4) + (bptr[2] >> 4)]; 

136 
if(len == 3) { 

137 
*eptr++ = __b32[(bptr[2] & 0xf) << 1]; 

138 
*eptr++ = '='; 

139 
*eptr++ = '='; 

140 
} 

141 
else { 

142 
*eptr++ = __b32[((bptr[2] & 0xf) << 1) + (bptr[3] >> 7)]; 

143 
*eptr++ = __b32[(bptr[3] & 0x7c) >> 2]; 

144 
*eptr++ = __b32[(bptr[3] & 0x3) << 3]; 

145 
} 

146 
} 

147 
} 

148 
*eptr = '='; 

149 
} 

150 
return n; 

151 
} 

152 

