[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r4285 - GNUnet/src/applications/fs/ecrs
From: |
grothoff |
Subject: |
[GNUnet-SVN] r4285 - GNUnet/src/applications/fs/ecrs |
Date: |
Thu, 11 Jan 2007 00:09:37 -0800 (PST) |
Author: grothoff
Date: 2007-01-11 00:09:35 -0800 (Thu, 11 Jan 2007)
New Revision: 4285
Modified:
GNUnet/src/applications/fs/ecrs/uri.c
Log:
implemented enc2bin, bin2enc
Modified: GNUnet/src/applications/fs/ecrs/uri.c
===================================================================
--- GNUnet/src/applications/fs/ecrs/uri.c 2007-01-11 07:48:15 UTC (rev
4284)
+++ GNUnet/src/applications/fs/ecrs/uri.c 2007-01-11 08:09:35 UTC (rev
4285)
@@ -79,7 +79,7 @@
* <p>
*
* TODO:
- * - bin2enc, enc2bin
+ * - test bin2enc, enc2bin
* - test conversion of LOC URIs from and to strings!
* - verify LOC signatures
*/
@@ -167,6 +167,11 @@
}
/**
+ * 64 characters for encoding, 6 bits per character
+ */
+static char * encTable__ =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
+
+/**
* Convert binary data to a string.
*
* @return converted data
@@ -174,10 +179,49 @@
static char *
bin2enc(const void * data,
size_t size) {
- /* FIXME */
- return STRDUP("");
+ size_t len;
+ size_t pos;
+ unsigned int bits;
+ unsigned int hbits;
+ char * ret;
+
+ GE_ASSERT(NULL, strlen(encTable__) == 64);
+ len = size * 8 / 6;
+ if (((size * 8) % 6) != 0)
+ len++;
+ ret = MALLOC(len+1);
+ ret[len] = '\0';
+ len = 0;
+ bits = 0;
+ hbits = 0;
+ for (pos=0;pos<size;pos++) {
+ bits |= ((((const unsigned char*)data)[pos]) << hbits);
+ hbits += 8;
+ while (hbits >= 6) {
+ ret[len++] = encTable__[bits & 63];
+ bits >>= 6;
+ hbits -= 6;
+ }
+ }
+ if (hbits > 0)
+ ret[len++] = encTable__[bits & 63];
+ return ret;
}
+static unsigned int getValue__(unsigned char a) {
+ if ( (a >= '0') && (a <= '9') )
+ return a - '0';
+ if ( (a >= 'A') && (a <= 'Z') )
+ return (a - 'A' + 10);
+ if ( (a >= 'a') && (a <= 'z') )
+ return (a - 'A' + 36);
+ if (a == '_')
+ return 60;
+ if (a == '=')
+ return 61;
+ return -1;
+}
+
/**
* Convert string back to binary data.
*
@@ -191,8 +235,30 @@
enc2bin(const char * input,
void * data,
size_t size) {
- /* FIXME */
- return -1;
+ size_t len;
+ size_t pos;
+ unsigned int bits;
+ unsigned int hbits;
+
+ len = size * 8 / 6;
+ if (((size * 8) % 6) != 0)
+ len++;
+ if (strlen(input) < len)
+ return -1; /* error! */
+ bits = 0;
+ hbits = 0;
+ len = 0;
+ pos = 0;
+ for (pos=0;pos<size;pos++) {
+ while (hbits < 8) {
+ bits |= (getValue__(input[len++]) << hbits);
+ hbits += 6;
+ }
+ (((unsigned char*)data)[pos]) = (unsigned char) bits;
+ bits >>= 8;
+ hbits -= 8;
+ }
+ return len;
}
/**
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r4285 - GNUnet/src/applications/fs/ecrs,
grothoff <=