source: EcnlProtoTool/trunk/ntshell/webserver/base64.c@ 442

Last change on this file since 442 was 442, checked in by coas-nagasima, 4 years ago

ntshellアプリはnewlibを使うよう変更し、syscallの実装部分と区別がつくよう更新。

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 3.2 KB
Line 
1/*
2 * Copyright(c) 2010, Norio Kobota, All rights reserved.
3 */
4
5#include <string.h>
6#include <stdlib.h>
7
8#include "base64.h"
9#include "core/ntlibc.h"
10
11static const char base64_encode_chars[] =
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 "abcdefghijklmnopqrstuvwxyz"
14 "0123456789+/";
15
16static const signed char base64_decode_chars[] = {
17 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
18 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
19 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
20 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
21 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
22 - 1, - 1, - 1, 62, - 1, - 1, - 1, 63,
23 52, 53, 54, 55, 56, 57, 58, 59,
24 60, 61, - 1, - 1, - 1, 0, - 1, - 1,
25 - 1, 0, 1, 2, 3, 4, 5, 6,
26 7, 8, 9, 10, 11, 12, 13, 14,
27 15, 16, 17, 18, 19, 20, 21, 22,
28 23, 24, 25, - 1, - 1, - 1, - 1, - 1,
29 - 1, 26, 27, 28, 29, 30, 31, 32,
30 33, 34, 35, 36, 37, 38, 39, 40,
31 41, 42, 43, 44, 45, 46, 47, 48,
32 49, 50, 51, - 1, - 1, - 1, - 1, - 1,
33 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
34 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
35 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
36 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
37 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
38 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
39 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
40 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
41 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
42 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
43 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
44 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
45 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
46 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
47 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
48 - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1};
49
50int base64_encode(unsigned char *dst, size_t dstsiz, const unsigned char *src, size_t srcsiz) {
51 unsigned long x = 0UL;
52 int i = 0, l = 0;
53 unsigned char *pdst;
54
55 pdst = dst;
56 for (; srcsiz > 0; src++, srcsiz--) {
57 x = x << 8 | *src;
58 for (l += 8; l >= 6; l -= 6) {
59 if (i >= dstsiz)
60 return -1;
61 pdst[i++] = base64_encode_chars[(x >> (l - 6)) & 0x3f];
62 }
63 }
64 if (l > 0) {
65 x <<= 6 - l;
66 if (i >= dstsiz)
67 return -1;
68 pdst[i++] = base64_encode_chars[x & 0x3f];
69 }
70 for (; i % 4;) {
71 if (i >= dstsiz)
72 return -1;
73 pdst[i++] = '=';
74 }
75 if (i >= dstsiz)
76 return -1;
77 pdst[i] = '\0';
78 return i;
79}
80
81int base64_decode(unsigned char *dst, size_t dstsiz, const unsigned char *src) {
82 union {
83 unsigned long x;
84 char c[4];
85 } base64;
86 unsigned char *pdst;
87 int i, j = 0, k = 0;
88 size_t srcsiz = strlen((const char *)src);
89
90 if ((srcsiz % 4) != 0) {
91 return -1;
92 }
93 base64.x = 0UL;
94 pdst = dst;
95 for (; srcsiz > 0; src+=4, srcsiz-=4) {
96 for (i = 0; i < 4; i++) {
97 if (base64_decode_chars[src[i]] == -1) {
98 return -1;
99 }
100 base64.x = base64.x << 6 | base64_decode_chars[src[i]];
101 j += (src[i] == '=');
102 }
103 for (i = 3; i > j; i--, k++) {
104 if (k >= dstsiz)
105 return -1;
106 *pdst++ = base64.c[i - 1];
107 }
108 }
109 if (k >= dstsiz)
110 return -1;
111 *pdst = '\0';
112 return 0;
113}
Note: See TracBrowser for help on using the repository browser.