]> sjero.net Git - wget/commitdiff
[svn] Made base64 DATA and DEST pointers void*.
authorhniksic <devnull@localhost>
Tue, 20 Jun 2006 08:16:36 +0000 (01:16 -0700)
committerhniksic <devnull@localhost>
Tue, 20 Jun 2006 08:16:36 +0000 (01:16 -0700)
src/ChangeLog
src/utils.c
src/utils.h

index d7cfd22027b10fe3c39b1da4aea35a589fba4c20..c1edde44ab02a231b2ff42536b1da8bf4f674676 100644 (file)
@@ -1,3 +1,10 @@
+2006-06-20  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * utils.c (base64_encode): Made the DATA pointer void * so the
+       callers can pass it any kind of pointer (including both signed and
+       unsigned char pointers).
+       (base64_decode): Ditto for DEST.
+
 2006-06-19  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * utils.c (base64_encode): Would read past end of STR.
 2006-06-19  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * utils.c (base64_encode): Would read past end of STR.
index 0a2fc9f977549ad3e5127321f7af371e25ec3679..9ed8526a43bf6e66e9874d56a8b86ebaacbe4821 100644 (file)
@@ -1889,20 +1889,20 @@ xsleep (double seconds)
 
 #endif /* not WINDOWS */
 
 
 #endif /* not WINDOWS */
 
-/* Encode the string STR of length LENGTH to base64 format and place it
-   to B64STORE.  The output will be \0-terminated, and must point to a
-   writable buffer of at least 1+BASE64_LENGTH(length) bytes.  It
-   returns the length of the resulting base64 data, not counting the
-   terminating zero.
+/* Encode the octets in DATA of length LENGTH to base64 format,
+   storing the result to DEST.  The output will be zero-terminated,
+   and must point to a writable buffer of at least
+   1+BASE64_LENGTH(length) bytes.  The function returns the length of
+   the resulting base64 data, not counting the terminating zero.
 
 
-   This implementation will not emit newlines after 76 characters of
+   This implementation does not emit newlines after 76 characters of
    base64 data.  */
 
 int
    base64 data.  */
 
 int
-base64_encode (const char *str, int length, char *b64store)
+base64_encode (const void *data, int length, char *dest)
 {
   /* Conversion table.  */
 {
   /* Conversion table.  */
-  static char tbl[64] = {
+  static const char tbl[64] = {
     'A','B','C','D','E','F','G','H',
     'I','J','K','L','M','N','O','P',
     'Q','R','S','T','U','V','W','X',
     'A','B','C','D','E','F','G','H',
     'I','J','K','L','M','N','O','P',
     'Q','R','S','T','U','V','W','X',
@@ -1912,9 +1912,10 @@ base64_encode (const char *str, int length, char *b64store)
     'w','x','y','z','0','1','2','3',
     '4','5','6','7','8','9','+','/'
   };
     'w','x','y','z','0','1','2','3',
     '4','5','6','7','8','9','+','/'
   };
-  const unsigned char *s = (const unsigned char *) str;
-  const unsigned char *end = (const unsigned char *) str + length - 2;
-  char *p = b64store;
+  const unsigned char *s = data;
+  /* Theoretical ANSI violation when length < 3. */
+  const unsigned char *end = data + length - 2;
+  char *p = dest;
 
   /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
   for (; s < end; s += 3)
 
   /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
   for (; s < end; s += 3)
@@ -1944,7 +1945,7 @@ base64_encode (const char *str, int length, char *b64store)
   /* ...and zero-terminate it.  */
   *p = '\0';
 
   /* ...and zero-terminate it.  */
   *p = '\0';
 
-  return p - b64store;
+  return p - dest;
 }
 
 /* Store in C the next non-whitespace character from the string, or \0
 }
 
 /* Store in C the next non-whitespace character from the string, or \0
@@ -1956,16 +1957,16 @@ base64_encode (const char *str, int length, char *b64store)
 #define IS_ASCII(c) (((c) & 0x80) == 0)
 
 /* Decode data from BASE64 (pointer to \0-terminated text) into memory
 #define IS_ASCII(c) (((c) & 0x80) == 0)
 
 /* Decode data from BASE64 (pointer to \0-terminated text) into memory
-   pointed to by TO.  TO should be large enough to accomodate the
+   pointed to by DEST.  DEST should be large enough to accomodate the
    decoded data, which is guaranteed to be less than strlen(base64).
 
    decoded data, which is guaranteed to be less than strlen(base64).
 
-   Since TO is assumed to contain binary data, it is not
+   Since DEST is assumed to contain binary data, it is not
    NUL-terminated.  The function returns the length of the data
    written to TO.  -1 is returned in case of error caused by malformed
    base64 input.  */
 
 int
    NUL-terminated.  The function returns the length of the data
    written to TO.  -1 is returned in case of error caused by malformed
    base64 input.  */
 
 int
-base64_decode (const char *base64, char *to)
+base64_decode (const char *base64, void *dest)
 {
   /* Table of base64 values for first 128 characters.  Note that this
      assumes ASCII (but so does Wget in other places).  */
 {
   /* Table of base64 values for first 128 characters.  Note that this
      assumes ASCII (but so does Wget in other places).  */
@@ -1989,7 +1990,7 @@ base64_decode (const char *base64, char *to)
 #define IS_BASE64(c) ((IS_ASCII (c) && BASE64_CHAR_TO_VALUE (c) >= 0) || c == '=')
 
   const char *p = base64;
 #define IS_BASE64(c) ((IS_ASCII (c) && BASE64_CHAR_TO_VALUE (c) >= 0) || c == '=')
 
   const char *p = base64;
-  char *q = to;
+  char *q = dest;
 
   while (1)
     {
 
   while (1)
     {
@@ -2048,7 +2049,7 @@ base64_decode (const char *base64, char *to)
 #undef IS_BASE64
 #undef BASE64_CHAR_TO_VALUE
 
 #undef IS_BASE64
 #undef BASE64_CHAR_TO_VALUE
 
-  return q - to;
+  return q - (char *) dest;
 }
 
 #undef IS_ASCII
 }
 
 #undef IS_ASCII
index fc6057291f45f486ca8000ab11bbfd5961faab39..c877157d2fb6a1aabfbfcc00fbdefc46e92940f7 100644 (file)
@@ -118,8 +118,8 @@ void xsleep (double);
 /* How many bytes it will take to store LEN bytes in base64.  */
 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
 
 /* How many bytes it will take to store LEN bytes in base64.  */
 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
 
-int base64_encode (const char *, int, char *);
-int base64_decode (const char *, char *);
+int base64_encode (const void *, int, char *);
+int base64_decode (const char *, void *);
 
 void stable_sort (void *, size_t, size_t, int (*) (const void *, const void *));
 
 
 void stable_sort (void *, size_t, size_t, int (*) (const void *, const void *));