Browse Source

cmText.h/c : Added cmTextDecodeBase64().

master
Kevin Larke 8 years ago
parent
commit
c5eaaf54b0
2 changed files with 233 additions and 1 deletions
  1. 209
    0
      cmText.c
  2. 24
    1
      cmText.h

+ 209
- 0
cmText.c View File

@@ -1026,3 +1026,212 @@ cmChar_t*       cmTextOutdent( cmChar_t* s, unsigned outdent )
1026 1026
   return s;
1027 1027
      
1028 1028
 }
1029
+
1030
+unsigned cmTextDecodeBase64BufferByteCount( const char* xV, unsigned xN )
1031
+{
1032
+  if( xN % 4 != 0 )
1033
+    return cmInvalidCnt;
1034
+
1035
+  unsigned yN = xN / 4 * 3;
1036
+
1037
+  if( xV[xN-1] == '=' )
1038
+    yN -= 1;
1039
+
1040
+  if( xV[xN-2] == '=' )
1041
+    yN -= 2;
1042
+
1043
+  return yN;  
1044
+}
1045
+
1046
+cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN )
1047
+{
1048
+  int t[] =
1049
+  {
1050
+   64, //  0
1051
+   64, //  1
1052
+   64, //  2
1053
+   64, //  3
1054
+   64, //  4
1055
+   64, //  5
1056
+   64, //  6
1057
+   64, //  7
1058
+   64, //  8
1059
+   64, //  9
1060
+   64, // 10
1061
+   64, // 11
1062
+   64, // 12
1063
+   64, // 13
1064
+   64, // 14
1065
+   64, // 15
1066
+   64, // 16
1067
+   64, // 17
1068
+   64, // 18
1069
+   64, // 19
1070
+   64, // 20
1071
+   64, // 21
1072
+   64, // 22
1073
+   64, // 23
1074
+   64, // 24
1075
+   64, // 25
1076
+   64, // 26
1077
+   64, // 27
1078
+   64, // 28
1079
+   64, // 29
1080
+   64, // 30
1081
+   64, // 31
1082
+   64, // 32
1083
+   64, // 33
1084
+   64, // 34
1085
+   64, // 35
1086
+   64, // 36
1087
+   64, // 37
1088
+   64, // 38
1089
+   64, // 39
1090
+   64, // 40
1091
+   64, // 41
1092
+   64, // 42
1093
+   62, // 43 +
1094
+   64, // 44
1095
+   64, // 45
1096
+   64, // 46
1097
+   63, // 47 /
1098
+   52, // 48 0
1099
+   53, // 49 1
1100
+   54, // 50 2
1101
+   55, // 51 3
1102
+   56, // 52 4
1103
+   57, // 53 5
1104
+   58, // 54 6
1105
+   59, // 55 7
1106
+   60, // 56 8
1107
+   61, // 57 9
1108
+   64, // 58
1109
+   64, // 59
1110
+   64, // 60
1111
+   64, // 61
1112
+   64, // 62
1113
+   64, // 63
1114
+   64, // 64
1115
+    0, // 65 A
1116
+    1, // 66 B
1117
+    2, // 67 C
1118
+    3, // 68 D
1119
+    4, // 69 E
1120
+    5, // 70 F
1121
+    6, // 71 G
1122
+    7, // 72 H
1123
+    8, // 73 I
1124
+    9, // 74 J
1125
+   10, // 75 K
1126
+   11, // 76 L
1127
+   12, // 77 M
1128
+   13, // 78 N
1129
+   14, // 79 O
1130
+   15, // 80 P
1131
+   16, // 81 Q
1132
+   17, // 82 R
1133
+   18, // 83 S
1134
+   19, // 84 T
1135
+   20, // 85 U
1136
+   21, // 86 V
1137
+   22, // 87 W
1138
+   23, // 88 X
1139
+   24, // 89 Y
1140
+   25, // 90 Z
1141
+   64, // 91
1142
+   64, // 92
1143
+   64, // 93
1144
+   64, // 94
1145
+   64, // 95
1146
+   64, // 96
1147
+   26, // 97 a
1148
+   27, // 98 b
1149
+   28, // 99 c
1150
+   29, //100 d
1151
+   30, //101 e
1152
+   31, //102 f
1153
+   32, //103 g
1154
+   33, //104 h
1155
+   34, //105 i
1156
+   35, //106 j
1157
+   36, //107 k
1158
+   37, //108 l
1159
+   38, //109 m
1160
+   39, //110 n
1161
+   40, //111 o
1162
+   41, //112 p
1163
+   42, //113 q
1164
+   43, //114 r
1165
+   44, //115 s
1166
+   45, //116 t
1167
+   46, //117 u 
1168
+   47, //118 v
1169
+   48, //119 w
1170
+   49, //120 x
1171
+   50, //121 y
1172
+   51, //122 z
1173
+   64, //123
1174
+   64, //124
1175
+   64, //125
1176
+   64, //126
1177
+   64  //127    
1178
+  };
1179
+  
1180
+  unsigned i  = 0;
1181
+  unsigned j  = 0;
1182
+  char*    zV = (char*)yV;
1183
+
1184
+  while( i < xN )
1185
+  {
1186
+    unsigned yn = 3;
1187
+    
1188
+    if( xV[i+3] == '=' )
1189
+      --yn;
1190
+    
1191
+    if( xV[i+2] == '=' )
1192
+      --yn;
1193
+
1194
+    unsigned v = 0;
1195
+    
1196
+    v += t[(int)xV[i++]] << 18;
1197
+    v += t[(int)xV[i++]] << 12;
1198
+    v += t[(int)xV[i++]] <<  6;
1199
+    v += t[(int)xV[i++]] <<  0;
1200
+
1201
+    if( j >= yN )
1202
+      break;
1203
+    
1204
+    zV[j++] = (v & 0xff0000) >> 16;
1205
+
1206
+    if( yn > 1 )
1207
+    {
1208
+      if( j >= yN )
1209
+        break;
1210
+      
1211
+      zV[j++] = (v & 0x00ff00) >> 8;
1212
+    }
1213
+    
1214
+    if( yn > 2 )
1215
+    {
1216
+      if( j >= yN )
1217
+        break;
1218
+      
1219
+      zV[j++] = (v & 0x0000ff) >> 0;
1220
+    }
1221
+    
1222
+  }
1223
+
1224
+  return j;
1225
+}
1226
+
1227
+
1228
+unsigned cmTextEncodeBase64BufferByteCount( unsigned binByteCnt )
1229
+{
1230
+  return 0;
1231
+}
1232
+
1233
+cmTxRC_t cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN )
1234
+{
1235
+  // const char* t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1236
+  return kOkTxRC;
1237
+}

+ 24
- 1
cmText.h View File

@@ -18,7 +18,8 @@ extern "C" {
18 18
     kOkTxRC = 0,
19 19
     kNullTxRC,
20 20
     kCvtErrTxRC,
21
-    kLHeapFailTxRC
21
+    kLHeapFailTxRC,
22
+    kBufTooSmallTxRC
22 23
   };
23 24
 
24 25
   typedef unsigned     cmTxRC_t;
@@ -273,6 +274,28 @@ extern "C" {
273 274
   // then all leading white space is removed.
274 275
   cmChar_t*       cmTextOutdent( cmChar_t* s, unsigned n );
275 276
 
277
+  // Given a string containing binary data encoded as base64
278
+  // return the size of the buffer required to hold the
279
+  // decoded binary data. Note that if xV[] is a legal base64
280
+  // string then xN must be a multiple of 4.  If xN is not
281
+  // a mulitple of 4 then the function returns kInvalidCnt.
282
+  unsigned cmTextDecodeBase64BufferByteCount( const char* xV, unsigned xN );
283
+
284
+  // Decode the base64 data in xV[xN] into yV[yN].  Note that the
285
+  // minimum value of yN can be determined via
286
+  // cmTextDecodeBase64BufferByteCount().
287
+  // Return the actual count of bytes decoded into yV[].
288
+  unsigned cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN );
289
+
290
+  // Given the count of bytes in a binary array, return
291
+  // the count of bytes required to store the array in base64.
292
+  unsigned cmTextEncodeBase64BufferByteCount( unsigned xN );
293
+
294
+  // Encode the binary array xV[xN] into yV[yN].  Note that the value
295
+  // of yN can be determined via cmTextEncodeBase64BufferByteCount().
296
+  cmTxRC_t cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN );
297
+
298
+
276 299
   //)
277 300
   //}
278 301
 

Loading…
Cancel
Save