Diff
Not logged in

Differences From:

File src/encode.c part of check-in [31e94c0a04] - Get "configuration push" working. Fix bugs in concealed-field processing of tickets. by drh on 2008-10-25 20:43:28. [view]

To:

File src/encode.c part of check-in [7a2c37063a] - merge trunk into creole branch by bob on 2009-09-22 07:49:39. Also file src/encode.c part of check-in [c198613ae1] - Update the base-64 encoder/decoder to conform to RFC 1421. Add test commands to verify both the endoder and the decoder. by drh on 2009-09-12 21:03:03. [view]

@@ -300,9 +300,9 @@
 /*
 ** The characters used for HTTP base64 encoding.
 */
 static unsigned char zBase[] =
-  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 /*
 ** Encode a string using a base-64 encoding.
 ** The encoding can be reversed using the <b>decode64</b> function.
@@ -315,9 +315,9 @@
 
   if( nData<=0 ){
     nData = strlen(zData);
   }
-  z64 = malloc( (nData*4)/3 + 6 );
+  z64 = malloc( (nData*4)/3 + 8 );
   for(i=n=0; i+2<nData; i+=3){
     z64[n++] = zBase[ (zData[i]>>2) & 0x3f ];
     z64[n++] = zBase[ ((zData[i]<<4) & 0x30) | ((zData[i+1]>>4) & 0x0f) ];
     z64[n++] = zBase[ ((zData[i+1]<<2) & 0x3c) | ((zData[i+2]>>6) & 0x03) ];
@@ -326,15 +326,33 @@
   if( i+1<nData ){
     z64[n++] = zBase[ (zData[i]>>2) & 0x3f ];
     z64[n++] = zBase[ ((zData[i]<<4) & 0x30) | ((zData[i+1]>>4) & 0x0f) ];
     z64[n++] = zBase[ ((zData[i+1]<<2) & 0x3c) ];
+    z64[n++] = '=';
   }else if( i<nData ){
     z64[n++] = zBase[ (zData[i]>>2) & 0x3f ];
     z64[n++] = zBase[ ((zData[i]<<4) & 0x30) ];
+    z64[n++] = '=';
+    z64[n++] = '=';
   }
   z64[n] = 0;
   return z64;
 }
+
+/*
+** COMMAND: test-encode64
+** Usage: %fossil test-encode64 STRING
+*/
+void test_encode64_cmd(void){
+  char *z;
+  int i;
+  for(i=2; i<g.argc; i++){
+    z = encode64(g.argv[i], -1);
+    printf("%s\n", z);
+    free(z);
+  }
+}
+
 
 /*
 ** This function treats its input as a base-64 string and returns the
 ** decoded value of that string.  Characters of input that are not
@@ -385,8 +403,22 @@
   return zData;
 }
 
 /*
+** COMMAND: test-decode64
+** Usage: %fossil test-decode64 STRING
+*/
+void test_decode64_cmd(void){
+  char *z;
+  int i, n;
+  for(i=2; i<g.argc; i++){
+    z = decode64(g.argv[i], &n);
+    printf("%d: %s\n", n, z);
+    free(z);
+  }
+}
+
+/*
 ** The base-16 encoding using the following characters:
 **
 **         0123456789abcdef
 **
@@ -424,8 +456,16 @@
   64, 10, 11, 12, 13, 14, 15, 64,  64,  1, 64, 64,  1, 64, 64,  0,
   64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
   64, 10, 11, 12, 13, 14, 15, 64,  64,  1, 64, 64,  1, 64, 64,  0,
   64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
+  64, 64, 64, 64, 64, 64, 64, 64,  64, 64, 64, 64, 64, 64, 64, 64,
 };
 
 /*
 ** Decode a N-character base-16 number into base-256.  N must be a
@@ -451,12 +491,13 @@
 ** Return true if the input string contains only valid base-16 digits.
 ** If any invalid characters appear in the string, return false.
 */
 int validate16(const char *zIn, int nIn){
-  int c, i;
-  for(i=0; i<nIn && (c = zIn[i])!=0; i++){
-    if( c & ~0x7f ) return 0;
-    if( zDecode[c]>63 ) return 0;
+  int i;
+  for(i=0; i<nIn; i++, zIn++){
+    if( zDecode[zIn[0]&0xff]>63 ){
+      return zIn[0]==0;
+    }
   }
   return 1;
 }