httpnEncode64 function in addition to httpEncode64 #860

Closed
michaelrsweet opened this Issue Aug 18, 2004 · 5 comments

Comments

Projects
None yet
1 participant
Collaborator

michaelrsweet commented Aug 18, 2004

Version: 1.1.21rc1
CUPS.org User: twaugh.redhat

(Colin Walters says:)

The CUPS httpEncode64 function isn't all that useful for what I'm doing,
since it only works on NULL-terminated strings, and I need to pass in
binary data. Here's a patch which adds a new httpnEncode64 function,
and defines httpEncode64 in terms of it.

Collaborator

michaelrsweet commented Aug 18, 2004

CUPS.org User: mike

OK, we won't accept this patch as-is, as it does not conform to the CMP coding standards (wrong indentation, no documentation added to the top of the http.c file, wrong function name prefix).

I'm thinking that we want to roll this into the new httpEncode64_2() function in 1.1.21 which adds a length parameter for the output buffer. It would be trivial to add a length parameter for the input buffer as well, since 1.1.21 hasn't been officially released.

The same will be true for the httpDecode64_2() function, so that it will be possible to decode arbitrary binary data as well...

Collaborator

michaelrsweet commented Aug 18, 2004

CUPS.org User: mike

Please let me know if you run into any problems with the patch...

Collaborator

michaelrsweet commented Aug 18, 2004

CUPS.org User: mike

Fixed in CVS - the anonymous CVS repository will be updated at midnight EST.

Collaborator

michaelrsweet commented Aug 28, 2004

"cups-1.1.21-httpnEncode64.patch":

--- cups-1.1.21rc1/cups/http.c~ 2004-08-17 10:26:39.705383344 -0400
+++ cups-1.1.21rc1/cups/http.c 2004-08-17 19:01:53.909709936 -0400
@@ -1784,17 +1784,32 @@
httpEncode64(char out, / I - String to write to /
const char *in) /
I - String to read from */
{

  • char outptr; / Output pointer */
  • static const char base64[] = /* Base64 characters... */
  •   {
    
  •     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
  •     "abcdefghijklmnopqrstuvwxyz"
    
  •     "0123456789"
    
  •     "+/"
    
  •   };
    
  • return httpnEncode64(out, in, strlen(in));
    +}

+/*

  • * 'httpnEncode64()' - Base64-encode a buffer.
  • */

+char * /* O - Encoded string /
+httpnEncode64(char *output, /
I - String to write to */

  •     const char _input,    /_ I - String to read from */
    
  •     unsigned int input_len)   /\* I - Integer input length */
    
    +{
  • unsigned char outptr; / Output pointer */
  • unsigned char *out = (unsigned char *) output;
  • const unsigned char *in = (const unsigned char *) input;
  • static const char base64[] = /* Base64 characters... */
  • {
  •  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
  •  "abcdefghijklmnopqrstuvwxyz"
    
  •  "0123456789"
    
  •  "+/"
    
  • };
  • for (outptr = out; *in != '\0'; in ++)
  • outptr = out;

  • while (input_len)
    {
    /*

  • Encode the up to 3 characters as 4 Base64 numbers...
    @@ -1804,7 +1819,8 @@
    *outptr ++ = base64[((in[0] << 4) | (in[1] >> 4)) & 63];

    in ++;

  • if (*in == '\0')

  • input_len--;

  • if (input_len == 0)
    {
    *outptr ++ = '=';
    *outptr ++ = '=';
    @@ -1814,13 +1830,16 @@
    *outptr ++ = base64[((in[0] << 2) | (in[1] >> 6)) & 63];

in ++;

  • if (*in == '\0')
  • input_len--;
  • if (input_len == 0)
    {
    *outptr ++ = '=';
    break;
    }

*outptr ++ = base64[in[0] & 63];

  • in++;
  • input_len--;
    }

*outptr = '\0';
@@ -1829,10 +1848,9 @@

  • Return the encoded string...
    */
  • return (out);
  • return (output);
    }

/*

  • 'httpGetLength()' - Get the amount of data remaining from the
  •                 content-length or transfer-encoding fields.
    
    --- cups-1.1.21rc1/cups/http.h~ 2004-08-17 10:26:39.981341392 -0400
    +++ cups-1.1.21rc1/cups/http.h 2004-08-17 18:56:58.333644328 -0400
    @@ -347,6 +347,8 @@
    extern http_status_t httpUpdate(http_t *http);
    extern int httpWrite(http_t *http, const char *buffer, int length);
    extern char *httpEncode64(char *out, const char *in);
    +extern char *httpnEncode64(char *output, const char *input, unsigned int input_len);
    +
    extern char *httpDecode64(char *out, const char *in);
    extern int httpGetLength(http_t *http);
    extern char *httpMD5(const char *, const char *, const char *,
Collaborator

michaelrsweet commented Aug 28, 2004

"str860.patch":

Index: http.c

RCS file: /development/cvs/cups/cups/http.c,v
retrieving revision 1.137
retrieving revision 1.142
diff -u -r1.137 -r1.142
--- http.c 27 May 2004 18:17:54 -0000 1.137
+++ http.c 18 Aug 2004 15:36:34 -0000 1.142
@@ -15,9 +15,9 @@

  •   Attn: CUPS Licensing Information
    
  •   Easy Software Products
    
  •   44141 Airport View Drive, Suite 204
    
    • * Hollywood, Maryland 20636-3111 USA
    • * Hollywood, Maryland 20636-3142 USA
      *
    • * Voice: (301) 373-9603
    • * Voice: (301) 373-9600
  •   EMail: cups-info@cups.org
    
  •     WWW: http://www.cups.org
    
    @@ -55,7 +55,9 @@
  • httpGetDateTime() - Get a time value from a formatted date/time string.
  • httpUpdate() - Update the current HTTP state for incoming data.
  • httpDecode64() - Base64-decode a string.
  • * httpDecode64_2() - Base64-decode a string.
  • httpEncode64() - Base64-encode a string.
  • * httpEncode64_2() - Base64-encode a string.
  • httpGetLength() - Get the amount of data remaining from the
  •                      content-length or transfer-encoding fields.
    
  • http_field() - Return the field index for a field name.
    @@ -290,6 +292,8 @@
    void
    httpClose(http_t http) / I - Connection to close */
    {
  • DEBUG_printf(("httpClose(http=%p)\n", http));
if (!http)
return;

@@ -353,7 +357,10 @@
struct hostent hostaddr; / Host address data */

  • if (host == NULL)
  • DEBUG_printf(("httpConnectEncrypt(host="%s", port=%d, encrypt=%d)\n",
  •            host ? host : "(null)", port, encrypt));
    
  • if (!host)
    return (NULL);

httpInitialize();
@@ -1580,6 +1587,9 @@
if (http->status == HTTP_CONTINUE)
return (http->status);

  •  if (http->status < HTTP_BAD_REQUEST)
    
  •    http->digest_tries = 0;
    
    #ifdef HAVE_SSL
    if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls)
    {
    @@ -1701,16 +1711,50 @@
    • 'httpDecode64()' - Base64-decode a string.
      */

-char * /* O - Decoded string /
-httpDecode64(char *out, /
I - String to write to */

  •         const char _in)   /_ I - String to read from _/
    
    +char * /_ O - Decoded string /
    +httpDecode64(char *out, /
    I - String to write to */
  •         const char _in)       /_ I - String to read from */
    
    +{
  • int outlen; /* Output buffer length */
  • /*
  • * Use the old maximum buffer size for binary compatibility...
  • */
  • outlen = 512;
  • return (httpDecode64_2(out, &outlen, in));
    +}

+/*

  • * 'httpDecode64_2()' - Base64-decode a string.
  • /
    +
    +char * /
    O - Decoded string /
    +httpDecode64_2(char *out, /
    I - String to write to */
  •      int        _outlen,  /_ IO - Size of output string */
    
  •           const char _in)     /_ I  - String to read from */
    
    {
  • int pos, /* Bit position */
  • base64; /* Value of this character */
  • char outptr; / Output pointer */
  • int pos, /* Bit position */
  • base64; /* Value of this character */
  • char outptr, / Output pointer */
  • outend; / End of output buffer */
  • /*
  • * Range check input...
  • */
  • if (!out || !outlen || _outlen < 1 || !in || !_in)
  • return (NULL);
  • for (outptr = out, pos = 0; *in != '\0'; in ++)
  • /*
  • * Convert from base-64 to bytes...
  • */
  • for (outptr = out, outend = out + outlen - 1, pos = 0; *in != '\0'; in ++)
    {
    /

  • Decode this character into a number from 0 to 63...
    @@ -1738,21 +1782,27 @@
    switch (pos)
    {
    case 0 :

  •      *outptr = base64 << 2;
    
  •      if (outptr < outend)
    
  •        *outptr = base64 << 2;
    

    pos ++;
    break;
    case 1 :

  •      *outptr++ |= (base64 >> 4) & 3;
    
  • *outptr = (base64 << 4) & 255;
    
  •      if (outptr < outend)
    
  •        *outptr++ |= (base64 >> 4) & 3;
    
  •      if (outptr < outend)
    
  •   *outptr = (base64 << 4) & 255;
    

    pos ++;
    break;
    case 2 :

  •      *outptr++ |= (base64 >> 2) & 15;
    
  • *outptr = (base64 << 6) & 255;
    
  •      if (outptr < outend)
    
  •        *outptr++ |= (base64 >> 2) & 15;
    
  •      if (outptr < outend)
    
  •   *outptr = (base64 << 6) & 255;
    

    pos ++;
    break;
    case 3 :

  •      *outptr++ |= base64;
    
  •      if (outptr < outend)
    
  •        *outptr++ |= base64;
    

    pos = 0;
    break;
    }
    @@ -1761,9 +1811,11 @@
    *outptr = '\0';

    /*

  • * Return the decoded string...

    • Return the decoded string and size...
      */
  • *outlen = (int)(outptr - out);

return (out);
}

@@ -1772,12 +1824,27 @@

  • 'httpEncode64()' - Base64-encode a string.
    */

-char * /* O - Encoded string /
-httpEncode64(char *out, /
I - String to write to */

  •         const char _in)   /_ I - String to read from _/
    
    +char * /_ O - Encoded string /
    +httpEncode64(char *out, /
    I - String to write to */
  •         const char _in)       /_ I - String to read from */
    
    +{
  • return (httpEncode64_2(out, 512, in, strlen(in)));
    +}

+/*

  • * 'httpEncode64_2()' - Base64-encode a string.
  • /
    +
    +char * /
    O - Encoded string /
    +httpEncode64_2(char *out, /
    I - String to write to */
  •      int        outlen,   /\* I - Size of output string */
    
  •           const char _in,     /_ I - String to read from */
    
  •      int        inlen)    /\* I - Size of input string */
    
    {
  • char outptr; / Output pointer */
  • static const char base64[] = /* Base64 characters... */
  • char outptr, / Output pointer */
  •   _outend;        /_ End of output buffer */
    
  • static const char base64[] = /* Base64 characters... */
    {
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    @@ -1786,33 +1853,53 @@
    };
  • for (outptr = out; *in != '\0'; in ++)
  • /*
  • * Range check input...
  • */
  • if (!out || outlen < 1 || !in || inlen < 1)
  • return (NULL);
  • /*
  • * Convert bytes to base-64...
  • */
  • for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
    {
    /*

  • Encode the up to 3 characters as 4 Base64 numbers...
    */

  • *outptr ++ = base64[in[0] >> 2];

  • *outptr ++ = base64[((in[0] << 4) | (in[1] >> 4)) & 63];

  • if (outptr < outend)

  •  *outptr ++ = base64[(in[0] & 255) >> 2];
    
  • if (outptr < outend)

  •  *outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
    

    in ++;

  • if (*in == '\0')

  • inlen --;

  • if (inlen <= 0)
    {

  •  *outptr ++ = '=';
    
  •  *outptr ++ = '=';
    
  •  if (outptr < outend)
    
  •    *outptr ++ = '=';
    
  •  if (outptr < outend)
    
  •    *outptr ++ = '=';
    

    break;
    }

  • *outptr ++ = base64[((in[0] << 2) | (in[1] >> 6)) & 63];

  • if (outptr < outend)

  •  *outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
    

    in ++;

  • if (*in == '\0')

  • inlen --;

  • if (inlen <= 0)
    {

  •  *outptr ++ = '=';
    
  •  if (outptr < outend)
    
  •    *outptr ++ = '=';
    

    break;
    }

  • *outptr ++ = base64[in[0] & 63];

  • if (outptr < outend)

  •  *outptr ++ = base64[in[0] & 63];
    

    }

    *outptr = '\0';

    Index: http.h

    RCS file: /development/cvs/cups/cups/http.h,v
    retrieving revision 1.48
    retrieving revision 1.51
    diff -u -r1.48 -r1.51
    --- http.h 25 Feb 2004 20:14:51 -0000 1.48
    +++ http.h 18 Aug 2004 15:34:02 -0000 1.51
    @@ -16,9 +16,9 @@

    •   Attn: CUPS Licensing Information
      
    •   Easy Software Products
      
    •   44141 Airport View Drive, Suite 204
      
  • * Hollywood, Maryland 20636-3111 USA

  • * Hollywood, Maryland 20636-3142 USA
    *

  • * Voice: (301) 373-9603

  • * Voice: (301) 373-9600

    •   EMail: cups-info@cups.org
      
    •     WWW: http://www.cups.org
      
      @@ -354,6 +354,16 @@
      extern void httpSetCookie(http_t *http, const char *cookie);
      extern int httpWait(http_t *http, int msec);

+/**** New in CUPS 1.1.21 ****/
+extern char *httpDecode64_2(char *out, int *outlen, const char *in);
+extern char *httpEncode64_2(char *out, int outlen, const char *in,

  •                       int inlen);
    

    +extern void httpSeparate2(const char *uri,

  •                     char *method, int methodlen,
    
  •                     char *username, int usernamelen,
    
  •                 char *host, int hostlen, int *port,
    
  •                 char *resource, int resourcelen);
    

    /*

    • C++ magic...

michaelrsweet added this to the Stable milestone Mar 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment