close Warning: Can't use blame annotator:
No changeset 2 in the repository

source: svn/newcon3bcm2_21bu/BSEAV/lib/utils/bbase64.c @ 46

Last change on this file since 46 was 46, checked in by megakiss, 11 years ago

459Mhz로 OTC 주파수 변경

  • Property svn:executable set to *
File size: 3.8 KB
RevLine 
1/***************************************************************************
2 *     Copyright (c) 2006-2008, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bbase64.c $
11 * $brcm_Revision: 4 $
12 * $brcm_Date: 4/7/08 7:33p $
13 *
14 * Module Description:
15 *
16 * BASE64 encoder/decoder
17 *
18 * Revision History:
19 *
20 * $brcm_Log: /BSEAV/lib/utils/bbase64.c $
21 *
22 * 4   4/7/08 7:33p vsilyaev
23 * PR 23826: Fixed typo in bbase64_encode
24 *
25 * 3   4/7/08 10:55a vsilyaev
26 * PR 23826: Fixed typo in termination code for bbase64_encode
27 *
28 * 2   4/7/08 10:47a vsilyaev
29 * PR 23826: Fixed typo in termination code for bbase64_encode
30 *
31 * 1   8/22/06 4:40p vsilyaev
32 * PR 23826: base64 encoder and decoder
33 *
34 *******************************************************************************/
35#include "bstd.h"
36#include "bbase64.h"
37
38BDBG_MODULE(bbase64);
39
40static const char b_base64_set[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41
42int
43bbase64_encode(const uint8_t *src, size_t src_len, char *dest, size_t dst_len)
44{
45        size_t src_off;
46        size_t dst_off;
47        unsigned acc;
48
49        for(src_off=dst_off=0;(src_off+3)<=src_len && (dst_off+4)<=dst_len; src_off+=3,dst_off+=4) {
50                acc =  (((unsigned)src[src_off+0])<<16) | (((unsigned)src[src_off+1])<<8) | src[src_off+2];
51                dest[dst_off+0] = b_base64_set[(acc>>(24-6))&0x3F];
52                dest[dst_off+1] = b_base64_set[(acc>>(24-12))&0x3F];
53                dest[dst_off+2] = b_base64_set[(acc>>(24-18))&0x3F];
54                dest[dst_off+3] = b_base64_set[acc&0x3F];
55        }
56        switch(src_len - src_off) {
57        default:
58                return -1;
59        case 0:
60                return  (int)dst_off;
61        case 1:
62                if ((dst_off+4)>dst_len) {
63                        return -1;
64                }
65                acc =  (((unsigned)src[src_off+0])<<16);
66                dest[dst_off+0] = b_base64_set[(acc>>(24-6))&0x3F];
67                dest[dst_off+1] = b_base64_set[(acc>>(24-12))&0x3F];
68                dest[dst_off+2] = '=';
69                dest[dst_off+3] = '=';
70                dst_off+=4;
71                break;
72        case 2:
73                if ((dst_off+4)>dst_len) {
74                        return -1;
75                }
76                acc =  (((unsigned)src[src_off+0])<<16) | (((unsigned)src[src_off+1])<<8);
77                dest[dst_off+0] = b_base64_set[(acc>>(24-6))&0x3F];
78                dest[dst_off+1] = b_base64_set[(acc>>(24-12))&0x3F];
79                dest[dst_off+2] = b_base64_set[(acc>>(24-18))&0x3F];
80                dest[dst_off+3] = '=';
81                dst_off+=4;
82                break;
83        }
84        return  (int)dst_off;
85}
86
87static int
88bbase64_decode_unit(char ch)
89{
90        int index;
91
92        if (ch >= 'A' && ch <= 'Z') {
93                index = ch - 'A';
94        } else if (ch >= 'a' && ch <= 'z') {
95                index = 26+(ch - 'a');
96        } else if (ch >= '0' && ch <= '9') {
97                index = 52+(ch - '0');
98        } else if (ch=='+') {
99                index = 62;
100        } else if (ch=='/') {
101                index = 63;
102        } else if (ch=='=') {
103                return 0;
104        } else {
105                return -1;
106        }
107        BDBG_MSG(("%c -> %d", ch, index));
108        BDBG_ASSERT(b_base64_set[index]==ch);
109        return index;
110}
111
112int
113bbase64_decode(const char *src, size_t src_len, uint8_t *dest, size_t dst_len)
114{
115        size_t src_off;
116        size_t dst_off;
117        unsigned acc;
118
119        for(src_off=dst_off=0;(src_off+4)<=src_len && (dst_off+3)<=dst_len; src_off+=4,dst_off+=3) {
120                int index0,index1,index2,index3;
121                if (src[src_off+0]=='=' || src[src_off+1]=='=') {
122                        return -1;
123                }
124                index0 = bbase64_decode_unit(src[src_off+0]);
125                index1 = bbase64_decode_unit(src[src_off+1]);
126                index2 = bbase64_decode_unit(src[src_off+2]);
127                index3 = bbase64_decode_unit(src[src_off+3]);
128                if(index0<0 || index1<0 || index2<0 || index3<0) {
129                        return -1;
130                }
131                acc = (index0 <<(3*6)) | (index1 <<(2*6)) | (index2 <<(1*6)) |  index3;
132                BDBG_MSG(("%#x", acc));
133                dest[dst_off+0] = acc>>16;
134                if(src[src_off+2]=='=') {
135                        return dst_off+1;
136                }
137                dest[dst_off+1] = (acc>>8);
138                if(src[src_off+3]=='=') {
139                        return dst_off+2;
140                }
141                dest[dst_off+2] = acc;
142        }
143        return dst_off;
144}
145
Note: See TracBrowser for help on using the repository browser.