base64.c 10 KB

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