Differences From:
File
src/encode.c
part of check-in
[4c37130fde]
- Various small performance enhancements.
by
drh on
2009-08-27 18:33:43.
[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
@@ -382,8 +400,22 @@
}
zData[j] = 0;
*pnByte = j;
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: