base64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: base64.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2015-07-04
  7. 功能说明: base64解码实现文件.
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "head.h"
  12. #include "x509.h"
  13. #include "base64.h"
  14. #ifdef LINUX_KERNEL_APP
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/fs.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kthread.h>
  23. #include <linux/delay.h>
  24. #include <linux/wait.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. #include <asm/ioctls.h>
  28. #include <linux/syscalls.h>
  29. #include <termios.h>
  30. #else
  31. #include <linux/fs.h>
  32. #include <linux/sched.h>
  33. #include <linux/wait.h>
  34. #include <termios.h>
  35. #endif
  36. enum {
  37. BASE64_FLAG_UU_STOP = 0x100,
  38. /* Sign-extends to a value which never matches fgetc result: */
  39. BASE64_FLAG_NO_STOP_CHAR = 0x80,
  40. };
  41. /* Conversion table. for base 64 */
  42. static const char bb_uuenc_tbl_base64[65 + 1] = {
  43. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  44. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  45. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  46. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  47. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  48. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  49. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  50. '4', '5', '6', '7', '8', '9', '+', '/',
  51. '=' /* termination character */,
  52. '\0' /* needed for uudecode.c only */
  53. };
  54. /*
  55. * Encode bytes at S of length LENGTH to uuencode or base64 format and place it
  56. * to STORE. STORE will be 0-terminated, and must point to a writable
  57. * buffer of at least 1+BASE64_LENGTH(length) bytes.
  58. * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
  59. */
  60. void bb_uuencode(char *p, const void *src, int length, const char *tbl)
  61. {
  62. const unsigned char *s = src;
  63. /* Transform the 3x8 bits to 4x6 bits */
  64. while (length > 0) {
  65. unsigned s1, s2;
  66. /* Are s[1], s[2] valid or should be assumed 0? */
  67. s1 = s2 = 0;
  68. length -= 3; /* can be >=0, -1, -2 */
  69. if (length >= -1) {
  70. s1 = s[1];
  71. if (length >= 0)
  72. s2 = s[2];
  73. }
  74. *p++ = tbl[s[0] >> 2];
  75. *p++ = tbl[((s[0] & 3) << 4) + (s1 >> 4)];
  76. *p++ = tbl[((s1 & 0xf) << 2) + (s2 >> 6)];
  77. *p++ = tbl[s2 & 0x3f];
  78. s += 3;
  79. }
  80. /* Zero-terminate */
  81. *p = '\0';
  82. /* If length is -2 or -1, pad last char or two */
  83. while (length) {
  84. *--p = tbl[64];
  85. length++;
  86. }
  87. }
  88. void encode_base64(char *p, const void *src, int length)
  89. {
  90. bb_uuencode(p,src,length,bb_uuenc_tbl_base64);
  91. }
  92. /*
  93. * Decode base64 encoded string. Stops on '\0'.
  94. *
  95. * Returns: pointer to the undecoded part of source.
  96. * If points to '\0', then the source was fully decoded.
  97. * (*pp_dst): advanced past the last written byte.
  98. */
  99. const char* decode_base64(char **pp_dst, const char *src)
  100. {
  101. char *dst = *pp_dst;
  102. const char *src_tail;
  103. while (1) {
  104. unsigned char six_bit[4];
  105. int count = 0;
  106. /* Fetch up to four 6-bit values */
  107. src_tail = src;
  108. while (count < 4) {
  109. char *table_ptr;
  110. int ch;
  111. /* Get next _valid_ character.
  112. * bb_uuenc_tbl_base64[] contains this string:
  113. * 0 1 2 3 4 5 6
  114. * 01234567890123456789012345678901234567890123456789012345678901234
  115. * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  116. */
  117. do {
  118. ch = *src;
  119. if (ch == '\0') {
  120. if (count == 0) {
  121. /* Example:
  122. * If we decode "QUJD <NUL>", we want
  123. * to return ptr to NUL, not to ' ',
  124. * because we did fully decode
  125. * the string (to "ABC").
  126. */
  127. src_tail = src;
  128. }
  129. goto ret;
  130. }
  131. src++;
  132. table_ptr = strchr(bb_uuenc_tbl_base64, ch);
  133. //todo: add BASE64_FLAG_foo to die on bad char?
  134. } while (!table_ptr);
  135. /* Convert encoded character to decimal */
  136. ch = table_ptr - bb_uuenc_tbl_base64;
  137. /* ch is 64 if char was '=', otherwise 0..63 */
  138. if (ch == 64)
  139. break;
  140. six_bit[count] = ch;
  141. count++;
  142. }
  143. /* Transform 6-bit values to 8-bit ones.
  144. * count can be < 4 when we decode the tail:
  145. * "eQ==" -> "y", not "y NUL NUL".
  146. * Note that (count > 1) is always true,
  147. * "x===" encoding is not valid:
  148. * even a single zero byte encodes as "AA==".
  149. * However, with current logic we come here with count == 1
  150. * when we decode "==" tail.
  151. */
  152. if (count > 1)
  153. *dst++ = six_bit[0] << 2 | six_bit[1] >> 4;
  154. if (count > 2)
  155. *dst++ = six_bit[1] << 4 | six_bit[2] >> 2;
  156. if (count > 3)
  157. *dst++ = six_bit[2] << 6 | six_bit[3];
  158. /* Note that if we decode "AA==" and ate first '=',
  159. * we just decoded one char (count == 2) and now we'll
  160. * do the loop once more to decode second '='.
  161. */
  162. } /* while (1) */
  163. ret:
  164. *pp_dst = dst;
  165. return src_tail;
  166. }
  167. static void read_base64(int src_stream, int dst_stream, int flags)
  168. {
  169. /* Note that EOF _can_ be passed as exit_char too */
  170. #define exit_char ((int)(signed char)flags)
  171. #define uu_style_end (flags & BASE64_FLAG_UU_STOP)
  172. /* uuencoded files have 61 byte lines. Use 64 byte buffer
  173. * to process line at a time.
  174. */
  175. enum { BUFFER_SIZE = 64 };
  176. char in_buf[BUFFER_SIZE + 2];
  177. char out_buf[BUFFER_SIZE / 4 * 3 + 2];
  178. char *out_tail;
  179. const char *in_tail;
  180. int term_seen = 0;
  181. int in_count = 0;
  182. char ch = 0;
  183. int ret = 0;
  184. while (1) {
  185. while (in_count < BUFFER_SIZE) {
  186. ret = sys_read(src_stream, &ch, 1);
  187. if (ret != 1) {
  188. if (in_count == 0)
  189. return;
  190. term_seen = 1;
  191. break;
  192. }
  193. if (ret != 1) {
  194. term_seen = 1;
  195. break;
  196. }
  197. /* Prevent "====" line to be split: stop if we see '\n'.
  198. * We can also skip other whitespace and skirt the problem
  199. * of files with NULs by stopping on any control char or space:
  200. */
  201. if (ch <= ' ')
  202. break;
  203. in_buf[in_count++] = ch;
  204. }
  205. in_buf[in_count] = '\0';
  206. /* Did we encounter "====" line? */
  207. if (uu_style_end && strcmp(in_buf, "====") == 0)
  208. return;
  209. out_tail = out_buf;
  210. in_tail = decode_base64(&out_tail, in_buf);
  211. //fwrite(out_buf, (out_tail - out_buf), 1, dst_stream);
  212. sys_write(dst_stream, out_buf, (out_tail - out_buf));
  213. if (term_seen) {
  214. /* Did we consume ALL characters? */
  215. if (*in_tail == '\0')
  216. return;
  217. /* No */
  218. //bb_error_msg_and_die("truncated base64 input");
  219. rt_printf("truncated base64 input");
  220. return;
  221. }
  222. /* It was partial decode */
  223. in_count = strlen(in_tail);
  224. memmove(in_buf, in_tail, in_count);
  225. }
  226. }
  227. int base64file_to_binfile(char * base64file, char *binfile)
  228. {
  229. int fd_src;
  230. int fd_dst;
  231. char *file_data;
  232. struct stat s;
  233. int ret = 0;
  234. int len_begin = strlen(BEGIN_CERTIFICATE);
  235. char *p_begin;
  236. char *p_end;
  237. //获取文件属性
  238. if(sys_newstat(base64file, &s))
  239. {
  240. rt_printf("%s:sys_newstat error\n",__func__);
  241. return -1;
  242. }
  243. //分配内存
  244. file_data = rt_malloc(s.st_size+1);
  245. if(!file_data)
  246. {
  247. rt_printf("%s:rt_malloc error\n",__func__);
  248. return -1;
  249. }
  250. //打开文件
  251. fd_src = sys_open(base64file, O_RDONLY, 0);
  252. if(fd_src < 0)
  253. {
  254. rt_printf("%s:sys_open %s error\n",__func__ , base64file);
  255. rt_free(file_data);
  256. return -1;
  257. }
  258. //读取整个文件内容
  259. ret = sys_read(fd_src, file_data, s.st_size);
  260. if(ret!=s.st_size)
  261. {
  262. rt_printf("%s:sys_read error\n",__func__);
  263. sys_close(fd_src);
  264. rt_free(file_data);
  265. return -1;
  266. }
  267. //最后一个字节补0
  268. file_data[s.st_size]=0;
  269. //关闭文件
  270. sys_close(fd_src);
  271. //查找base64编码的开始字符串
  272. p_begin=strstr(file_data, BEGIN_CERTIFICATE);
  273. //查找base64编码的结束字符串
  274. p_end=strstr(file_data, END_CERTIFICATE);
  275. //找到开始标示和结束标示,说明是base64编码格式.需要将开始和结束标示处理掉
  276. if(p_begin && p_end)
  277. {
  278. //创建临时文件
  279. fd_src = sys_open(FILENAME_X509_TMP, O_CREAT|O_RDWR | O_TRUNC|O_SYNC, 0777);
  280. if(fd_src < 0)
  281. {
  282. rt_printf("%s:creat file %s error\n",__func__, FILENAME_X509_TMP);
  283. rt_free(file_data);
  284. return -1;
  285. }
  286. //base64编码的开始字符
  287. p_begin =&p_begin[len_begin];
  288. //将base64编码内容写到临时文件中,便于解码
  289. sys_write(fd_src, p_begin, (p_end - p_begin ));
  290. //关闭临时文件
  291. sys_close(fd_src);
  292. //临时文件创建成功后,再以只读方式打开
  293. fd_src = sys_open(FILENAME_X509_TMP, O_RDONLY, 0);
  294. if(fd_src < 0)
  295. {
  296. rt_printf("%s:sys_open %s error\n",__func__ , FILENAME_X509_TMP);
  297. rt_free(file_data);
  298. return -1;
  299. }
  300. //创建解码临时文件
  301. fd_dst=sys_open(binfile, O_CREAT|O_RDWR | O_TRUNC|O_SYNC, 0777);
  302. if(fd_dst < 0)
  303. {
  304. rt_printf("%s:creat file %s error\n",__func__, binfile);
  305. sys_close(fd_src);
  306. return -1;
  307. }
  308. //解码
  309. read_base64(fd_src, fd_dst, (char)-1);
  310. //关闭文件
  311. sys_close(fd_dst);
  312. sys_close(fd_src);
  313. }
  314. else
  315. {
  316. //如果是二进制文件,直接拷贝
  317. rt_file_cp(base64file, binfile);
  318. }
  319. //释放内存
  320. rt_free(file_data);
  321. return 0;
  322. }