nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 /**
3 * `encode.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 "b64.h"
34  
35 char *
36 b64_encode (const unsigned char *src, size_t len) {
37 int i = 0;
38 int j = 0;
39 char *enc = NULL;
40 size_t size = 0;
41 unsigned char buf[4];
42 unsigned char tmp[3];
43  
44 // alloc
45 enc = (char *) b64_malloc(1);
46 if (NULL == enc) { return NULL; }
47  
48 // parse until end of source
49 while (len--) {
50 // read up to 3 bytes at a time into `tmp'
51 tmp[i++] = *(src++);
52  
53 // if 3 bytes read then encode into `buf'
54 if (3 == i) {
55 buf[0] = (tmp[0] & 0xfc) >> 2;
56 buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
57 buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
58 buf[3] = tmp[2] & 0x3f;
59  
60 // allocate 4 new byts for `enc` and
61 // then translate each encoded buffer
62 // part by index from the base 64 index table
63 // into `enc' unsigned char array
64 enc = (char *) b64_realloc(enc, size + 4);
65 for (i = 0; i < 4; ++i) {
66 enc[size++] = b64_table[buf[i]];
67 }
68  
69 // reset index
70 i = 0;
71 }
72 }
73  
74 // remainder
75 if (i > 0) {
76 // fill `tmp' with `\0' at most 3 times
77 for (j = i; j < 3; ++j) {
78 tmp[j] = '\0';
79 }
80  
81 // perform same codec as above
82 buf[0] = (tmp[0] & 0xfc) >> 2;
83 buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
84 buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
85 buf[3] = tmp[2] & 0x3f;
86  
87 // perform same write to `enc` with new allocation
88 for (j = 0; (j < i + 1); ++j) {
89 enc = (char *) b64_realloc(enc, size + 1);
90 enc[size++] = b64_table[buf[j]];
91 }
92  
93 // while there is still a remainder
94 // append `=' to `enc'
95 while ((i++ < 3)) {
96 enc = (char *) b64_realloc(enc, size + 1);
97 enc[size++] = '=';
98 }
99 }
100  
101 // Make sure we have enough space to add '\0' character at end.
102 enc = (char *) b64_realloc(enc, size + 1);
103 enc[size] = '\0';
104  
105 return enc;
106 }