Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: base/security/b64.c

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/security/b64.h ('k') | base/security/build.scons » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2007-2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include "b64.h"
17
18 static const char b64outmap[64] = {
19 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
20 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
21 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
22 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
23 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
24 };
25
26 static const char b64inmap[96] = {
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 0, 0,
28 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0,
29 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
30 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 64,
31 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
32 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 0, 0, 0, 0, 0,
33 };
34
35 int B64_encode(const uint8_t* input,
36 int input_length,
37 char* output,
38 int output_max) {
39 unsigned int accu = 0;
40 int shift = 0;
41 int output_size = 0;
42
43 while (input_length--) {
44 accu <<= 8;
45 accu |= *input++;
46 shift += 8;
47
48 while (shift >= 6) {
49 if (output_size >= output_max) return -1; // out of output space
50 output[output_size++] = b64outmap[(accu >> (shift - 6)) & 63];
51 shift -= 6;
52 }
53 }
54 if (shift) {
55 if (output_size >= output_max) return -1; // out of output space
56 accu <<= 8; // pad with 0 byte really
57 shift += 8;
58 output[output_size++] = b64outmap[(accu >> (shift - 6)) & 63];
59 }
60
61 // Output terminating 0
62 if (output_size >= output_max) return -1; // out of output space
63 output[output_size] = '\0';
64
65 return output_size;
66 }
67
68 int B64_decode(const char* input,
69 uint8_t* output,
70 int output_max) {
71 unsigned int accu = 0;
72 int shift = 0;
73 int output_size = 0;
74
75 while (*input) {
76 unsigned char in = *input++ & 255;
77 if (in < 32 || in > 127 || !b64inmap[in - 32]) return -1; // invalid input
78 accu <<= 6;
79 accu |= (b64inmap[in - 32] - 1);
80 shift += 6;
81 if (shift >= 8) {
82 if (output_size >= output_max) return -1; // out of output space
83 output[output_size++] = (accu >> (shift - 8)) & 255;
84 shift -= 8;
85 }
86 }
87 return output_size;
88 }
OLDNEW
« no previous file with comments | « base/security/b64.h ('k') | base/security/build.scons » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698