nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 /**
3 * `decode.c' - b64
4 *
5 * copyright (c) 2014 joseph werle
6 *
7 * License:
8 * The MIT License (MIT)
9 *
10 * Copyright (c) 2014 Little Star Media, Inc.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in all
20 * copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
29 */
30  
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include "b64.h"
35  
36  
37 unsigned char *
38 b64_decode (const char *src, size_t len) {
39 return b64_decode_ex(src, len, NULL);
40 }
41  
42 unsigned char *
43 b64_decode_ex (const char *src, size_t len, size_t *decsize) {
44 int i = 0;
45 int j = 0;
46 int l = 0;
47 size_t size = 0;
48 unsigned char *dec = NULL;
49 unsigned char buf[3];
50 unsigned char tmp[4];
51  
52 // alloc
53 dec = (unsigned char *) b64_malloc(1);
54 if (NULL == dec) { return NULL; }
55  
56 // parse until end of source
57 while (len--) {
58 // break if char is `=' or not base64 char
59 if ('=' == src[j]) { break; }
60 if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }
61  
62 // read up to 4 bytes at a time into `tmp'
63 tmp[i++] = src[j++];
64  
65 // if 4 bytes read then decode into `buf'
66 if (4 == i) {
67 // translate values in `tmp' from table
68 for (i = 0; i < 4; ++i) {
69 // find translation char in `b64_table'
70 for (l = 0; l < 64; ++l) {
71 if (tmp[i] == b64_table[l]) {
72 tmp[i] = l;
73 break;
74 }
75 }
76 }
77  
78 // decode
79 buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
80 buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
81 buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
82  
83 // write decoded buffer to `dec'
84 dec = (unsigned char *) b64_realloc(dec, size + 3);
85 if (dec != NULL){
86 for (i = 0; i < 3; ++i) {
87 dec[size++] = buf[i];
88 }
89 } else {
90 return NULL;
91 }
92  
93 // reset
94 i = 0;
95 }
96 }
97  
98 // remainder
99 if (i > 0) {
100 // fill `tmp' with `\0' at most 4 times
101 for (j = i; j < 4; ++j) {
102 tmp[j] = '\0';
103 }
104  
105 // translate remainder
106 for (j = 0; j < 4; ++j) {
107 // find translation char in `b64_table'
108 for (l = 0; l < 64; ++l) {
109 if (tmp[j] == b64_table[l]) {
110 tmp[j] = l;
111 break;
112 }
113 }
114 }
115  
116 // decode remainder
117 buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
118 buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
119 buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
120  
121 // write remainer decoded buffer to `dec'
122 dec = (unsigned char *) b64_realloc(dec, size + (i - 1));
123 if (dec != NULL){
124 for (j = 0; (j < i - 1); ++j) {
125 dec[size++] = buf[j];
126 }
127 } else {
128 return NULL;
129 }
130 }
131  
132 // Make sure we have enough space to add '\0' character at end.
133 dec = (unsigned char *) b64_realloc(dec, size + 1);
134 if (dec != NULL){
135 dec[size] = '\0';
136 } else {
137 return NULL;
138 }
139  
140 // Return back the size of decoded string if demanded.
141 if (decsize != NULL) {
142 *decsize = size;
143 }
144  
145 return dec;
146 }