Browse Source

cmText.c : Implemented cmTextEncodeBase64().

master
Kevin Larke 9 years ago
parent
commit
fd59f5ae69
1 changed files with 62 additions and 4 deletions
  1. 62
    4
      cmText.c

+ 62
- 4
cmText.c View File

@@ -1192,6 +1192,8 @@ cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN
1192 1192
       --yn;
1193 1193
 
1194 1194
     unsigned v = 0;
1195
+
1196
+    assert( i + 4 <= xN );
1195 1197
     
1196 1198
     v += t[(int)xV[i++]] << 18;
1197 1199
     v += t[(int)xV[i++]] << 12;
@@ -1227,11 +1229,67 @@ cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN
1227 1229
 
1228 1230
 unsigned cmTextEncodeBase64BufferByteCount( unsigned binByteCnt )
1229 1231
 {
1230
-  return 0;
1232
+  int rem = binByteCnt % 3;
1233
+  binByteCnt -= rem;
1234
+
1235
+  int n = binByteCnt / 3 * 4;
1236
+
1237
+  if( rem )
1238
+    n += 4;
1239
+
1240
+  return n;
1241
+  
1231 1242
 }
1232 1243
 
1233
-cmTxRC_t cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN )
1244
+unsigned cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN )
1234 1245
 {
1235
-  // const char* t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1236
-  return kOkTxRC;
1246
+  const char*  t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1247
+  const char* zV = (const char*)xV;
1248
+  unsigned     i = 0;
1249
+  unsigned     j = 0;
1250
+  
1251
+  while( 1 )
1252
+  {
1253
+    unsigned k = 3;
1254
+    unsigned v = ((int)zV[i++]) << 16;
1255
+
1256
+    if( i < xN )
1257
+      v += ((int)zV[i++]) << 8;
1258
+    else
1259
+      --k;
1260
+
1261
+    if( i < xN )
1262
+      v += ((int)zV[i++]);
1263
+    else
1264
+      --k;
1265
+
1266
+    if( j >= yN )
1267
+      break;
1268
+    
1269
+    yV[j++] = t[ (v & 0xfc0000) >> 18 ];
1270
+
1271
+    if( j >= yN )
1272
+      break;
1273
+    
1274
+    yV[j++] = t[ (v & 0x03f000) >> 12 ];
1275
+
1276
+    if( j >= yN )
1277
+      break;
1278
+    
1279
+    if( k > 1 )
1280
+      yV[j++] = t[ (v & 0x000fc0) >>  6 ];
1281
+    else
1282
+      yV[j++] = '=';
1283
+
1284
+    if( j >= yN )
1285
+      break;
1286
+    
1287
+    if( k > 2 )
1288
+      yV[j++] = t[ (v & 0x00003f) >>  0 ];
1289
+    else
1290
+      yV[j++] = '=';
1291
+    
1292
+  }
1293
+  
1294
+  return j;
1237 1295
 }

Loading…
Cancel
Save