trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zw...@apache.org
Subject svn commit: r1227956 - in /trafficserver/traffic/trunk: lib/ts/ink_base64.cc lib/ts/ink_base64.h proxy/InkAPI.cc proxy/InkAPITest.cc proxy/api/ts/ts.h.in proxy/logging/LogUtils.cc
Date Fri, 06 Jan 2012 05:07:01 GMT
Author: zwoop
Date: Fri Jan  6 05:07:01 2012
New Revision: 1227956

URL: http://svn.apache.org/viewvc?rev=1227956&view=rev
Log:
Make some APIs more uniform, and fix some bugs from previous changes

Modified:
    trafficserver/traffic/trunk/lib/ts/ink_base64.cc
    trafficserver/traffic/trunk/lib/ts/ink_base64.h
    trafficserver/traffic/trunk/proxy/InkAPI.cc
    trafficserver/traffic/trunk/proxy/InkAPITest.cc
    trafficserver/traffic/trunk/proxy/api/ts/ts.h.in
    trafficserver/traffic/trunk/proxy/logging/LogUtils.cc

Modified: trafficserver/traffic/trunk/lib/ts/ink_base64.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/ts/ink_base64.cc?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/ts/ink_base64.cc (original)
+++ trafficserver/traffic/trunk/lib/ts/ink_base64.cc Fri Jan  6 05:07:01 2012
@@ -38,20 +38,17 @@
 #include "ink_resource.h"
 #include "ink_unused.h"
 
+// TODO: The code here seems a bit klunky, and could probably be improved a bit.
 
-// TODO: This code is not paritcularly "uniform", we ought to make them
-// more the same coding style (the uuencode one seems good'ish).
-
-
-size_t
-ats_base64_encode(const unsigned char *inBuffer, size_t inBufferSize, char *outBuffer, size_t
outBufSize)
+bool
+ats_base64_encode(const unsigned char *inBuffer, size_t inBufferSize, char *outBuffer, size_t
outBufSize, size_t *length)
 {
   static const char _codes[66] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
   char *obuf = outBuffer;
   char in_tail[4];
 
   if (outBufSize < ATS_BASE64_ENCODE_DSTLEN(inBufferSize))
-    return 0;
+    return false;
 
   while (inBufferSize > 2) {
     *obuf++ = _codes[(inBuffer[0] >> 2) & 077];
@@ -73,7 +70,8 @@ ats_base64_encode(const unsigned char *i
    */
   if (inBufferSize == 0) {
     *obuf = '\0';
-    return (obuf - outBuffer);
+    if (length)
+      *length = (obuf - outBuffer);
   } else {
     memset(in_tail, 0, sizeof(in_tail));
     memcpy(in_tail, inBuffer, inBufferSize);
@@ -88,14 +86,17 @@ ats_base64_encode(const unsigned char *i
     *(obuf + 3) = '=';
     *(obuf + 4) = '\0';
 
-    return ((obuf + 4) - outBuffer);
+    if (length)
+      *length = (obuf + 4) - outBuffer;
   }
+
+  return true;
 }
 
-size_t
-ats_base64_encode(const char *inBuffer, size_t inBufferSize, char *outBuffer, size_t outBufSize)
+bool
+ats_base64_encode(const char *inBuffer, size_t inBufferSize, char *outBuffer, size_t outBufSize,
size_t *length)
 {
-  return ats_base64_encode((const unsigned char *)inBuffer, inBufferSize, outBuffer, outBufSize);
+  return ats_base64_encode((const unsigned char *)inBuffer, inBufferSize, outBuffer, outBufSize,
length);
 }
 
 
@@ -124,23 +125,29 @@ const unsigned char printableToSixBit[25
   64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
 };
 
-size_t
-ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned char *outBuffer, size_t
outBufSize)
+bool
+ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned char *outBuffer, size_t
outBufSize, size_t *length)
 {
+  size_t inBytes = 0;
   size_t decodedBytes = 0;
-  unsigned char *outStart = outBuffer;
+  unsigned char *buf = outBuffer;
   int inputBytesDecoded = 0;
 
   // Make sure there is sufficient space in the output buffer
   if (outBufSize < ATS_BASE64_DECODE_DSTLEN(inBufferSize))
-    return 0;
+    return false;
 
-  for (size_t i = 0; i < inBufferSize; i += 4) {
-    outBuffer[0] = (unsigned char) (DECODE(inBuffer[0]) << 2 | DECODE(inBuffer[1])
>> 4);
-    outBuffer[1] = (unsigned char) (DECODE(inBuffer[1]) << 4 | DECODE(inBuffer[2])
>> 2);
-    outBuffer[2] = (unsigned char) (DECODE(inBuffer[2]) << 6 | DECODE(inBuffer[3]));
+  // Ignore any trailing ='s or other undecodable characters.
+  // TODO: Perhaps that ought to be an error instead?
+  while (printableToSixBit[(uint8_t)inBuffer[inBytes]] <= MAX_PRINT_VAL)
+    ++inBytes;
+
+  for (size_t i = 0; i < inBytes; i += 4) {
+    buf[0] = (unsigned char) (DECODE(inBuffer[0]) << 2 | DECODE(inBuffer[1]) >>
4);
+    buf[1] = (unsigned char) (DECODE(inBuffer[1]) << 4 | DECODE(inBuffer[2]) >>
2);
+    buf[2] = (unsigned char) (DECODE(inBuffer[2]) << 6 | DECODE(inBuffer[3]));
 
-    outBuffer += 3;
+    buf += 3;
     inBuffer += 4;
     decodedBytes += 3;
     inputBytesDecoded += 4;
@@ -148,14 +155,17 @@ ats_base64_decode(const char *inBuffer, 
 
   // Check to see if we decoded a multiple of 4 four
   //    bytes
-  if ((inBufferSize - inputBytesDecoded) & 0x3) {
+  if ((inBytes - inputBytesDecoded) & 0x3) {
     if (DECODE(inBuffer[-2]) > MAX_PRINT_VAL) {
       decodedBytes -= 2;
     } else {
       decodedBytes -= 1;
     }
   }
-  outStart[decodedBytes] = '\0';
+  outBuffer[decodedBytes] = '\0';
+
+  if (length)
+    *length = decodedBytes;
 
-  return decodedBytes;
+  return true;
 }

Modified: trafficserver/traffic/trunk/lib/ts/ink_base64.h
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/ts/ink_base64.h?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/ts/ink_base64.h (original)
+++ trafficserver/traffic/trunk/lib/ts/ink_base64.h Fri Jan  6 05:07:01 2012
@@ -37,10 +37,10 @@
  *
  */
 // Encodes / Decodes into user supplied buffer.  Returns number of bytes decoded
-size_t ats_base64_encode(const char *inBuffer, size_t inBufferSize, char *outBuffer, size_t
outBufSize);
-size_t ats_base64_encode(const unsigned char *inBuffer, size_t inBufferSize, char *outBuffer,
size_t outBufSize);
+bool ats_base64_encode(const char *inBuffer, size_t inBufferSize, char *outBuffer, size_t
outBufSize, size_t *length);
+bool ats_base64_encode(const unsigned char *inBuffer, size_t inBufferSize, char *outBuffer,
size_t outBufSize, size_t *length);
 
-size_t ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned char *outBuffer,
size_t outBufSize);
+bool ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned char *outBuffer,
size_t outBufSize, size_t *length);
 
 // Little helper functions to calculate minimum required output buffer for encoding/decoding.
 #define ATS_BASE64_ENCODE_DSTLEN(_length) ((_length * 8) / 6 + 4)

Modified: trafficserver/traffic/trunk/proxy/InkAPI.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/InkAPI.cc?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/InkAPI.cc (original)
+++ trafficserver/traffic/trunk/proxy/InkAPI.cc Fri Jan  6 05:07:01 2012
@@ -1684,22 +1684,22 @@ _TSfree(void *ptr)
 // Encoding utility
 //
 ////////////////////////////////////////////////////////////////////
-size_t
-TSBase64Decode(const char *input, size_t length, unsigned char *dst, size_t dst_size)
+TSReturnCode
+TSBase64Decode(const char *str, size_t str_len, unsigned char *dst, size_t dst_size, size_t
*length)
 {
-  sdk_assert(sdk_sanity_check_null_ptr((void*)input) == TS_SUCCESS);
+  sdk_assert(sdk_sanity_check_null_ptr((void*)str) == TS_SUCCESS);
   sdk_assert(sdk_sanity_check_null_ptr((void*)dst) == TS_SUCCESS);
 
-  return ats_base64_decode(input, length, dst, dst_size);
+  return ats_base64_decode(str, str_len, dst, dst_size, length) ? TS_SUCCESS : TS_ERROR;
 }
 
-size_t
-TSBase64Encode(const char *input, size_t length, char *dst, size_t dst_size)
+TSReturnCode
+TSBase64Encode(const char *str, size_t str_len, char *dst, size_t dst_size, size_t *length)
 {
-  sdk_assert(sdk_sanity_check_null_ptr((void*)input) == TS_SUCCESS);
+  sdk_assert(sdk_sanity_check_null_ptr((void*)str) == TS_SUCCESS);
   sdk_assert(sdk_sanity_check_null_ptr((void*)dst) == TS_SUCCESS);
 
-  return ats_base64_encode(input, length, dst, dst_size);
+  return ats_base64_encode(str, str_len, dst, dst_size, length) ? TS_SUCCESS : TS_ERROR;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -2307,7 +2307,6 @@ TSStringPercentEncode(const char* str, i
 {
   sdk_assert(sdk_sanity_check_null_ptr((void*)str) == TS_SUCCESS);
   sdk_assert(sdk_sanity_check_null_ptr((void*)dst) == TS_SUCCESS);
-  sdk_assert(sdk_sanity_check_null_ptr((void*)length) == TS_SUCCESS);
 
   int new_len; // Unfortunately, a lot of the core uses "int" for length's internally...
 
@@ -2318,42 +2317,45 @@ TSStringPercentEncode(const char* str, i
 
   // TODO: Perhaps we should make escapify_url() deal with const properly...
   if (NULL == LogUtils::escapify_url(NULL, const_cast<char*>(str), str_len, &new_len,
dst, dst_size, map)) {
-    *length = 0;
+    if (length)
+      *length = 0;
     return TS_ERROR;
   }
 
-  *length = new_len;
+  if (length)
+    *length = new_len;
+
   return TS_SUCCESS;
 }
 
-size_t
-TSStringPercentDecode(const char *src, size_t src_len, char* dst, size_t dst_size)
+TSReturnCode
+TSStringPercentDecode(const char *str, size_t str_len, char* dst, size_t dst_size, size_t
*length)
 {
-  sdk_assert(sdk_sanity_check_null_ptr((void*)src) == TS_SUCCESS);
+  sdk_assert(sdk_sanity_check_null_ptr((void*)str) == TS_SUCCESS);
   sdk_assert(sdk_sanity_check_null_ptr((void*)dst) == TS_SUCCESS);
 
-  if (src_len < 0)
-    src_len = strlen(src);
+  if (str_len < 0)
+    str_len = strlen(str);
 
   // return unescapifyStr(str);
   char *buffer = dst;
-  const char *str =  src;
+  const char *src =  str;
   int s = 0; // State, which we don't really use
 
-  unescape_str(buffer, buffer+dst_size, str, str+src_len, s);
+  // TODO: We should check for "failures" here?
+  unescape_str(buffer, buffer+dst_size, src, src+str_len, s);
   *buffer = '\0';
+  if (length)
+    *length = (buffer - dst);
 
-  return buffer - dst;
+  return TS_SUCCESS;
 }
 
-
-
 TSReturnCode
 TSUrlPercentEncode(TSMBuffer bufp, TSMLoc obj, char *dst, size_t dst_size, size_t *length,
const unsigned char *map)
 {
   sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
   sdk_assert(sdk_sanity_check_url_handle(obj) == TS_SUCCESS);
-  sdk_assert(sdk_sanity_check_null_ptr((void*)length) == TS_SUCCESS);
 
   char *url;
   int url_len;

Modified: trafficserver/traffic/trunk/proxy/InkAPITest.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/InkAPITest.cc?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/InkAPITest.cc (original)
+++ trafficserver/traffic/trunk/proxy/InkAPITest.cc Fri Jan  6 05:07:01 2012
@@ -7661,18 +7661,19 @@ REGRESSION_TEST(SDK_API_OVERRIDABLE_CONF
 }
 
 ////////////////////////////////////////////////
-// SDK_API_PERCENT_ENCODING
+// SDK_API_ENCODING
 //
 // Unit Test for API: TSStringPercentEncode
 //                    TSUrlPercentEncode
 //                    TSStringPercentDecode
 ////////////////////////////////////////////////
 
-REGRESSION_TEST(SDK_API_PERCENT_ENCODING) (RegressionTest * test, int atype, int *pstatus)
+REGRESSION_TEST(SDK_API_ENCODING) (RegressionTest * test, int atype, int *pstatus)
 {
   const char *url = "http://www.example.com/foo?fie= \"#%<>[]\\^`{}~&bar={test}&fum=Apache
Traffic Server";
   const char *url_encoded =
     "http://www.example.com/foo?fie=%20%22%23%25%3C%3E%5B%5D%5C%5E%60%7B%7D%7E&bar=%7Btest%7D&fum=Apache%20Traffic%20Server";
+  const char *url_base64 = "aHR0cDovL3d3dy5leGFtcGxlLmNvbS9mb28/ZmllPSAiIyU8PltdXF5ge31+JmJhcj17dGVzdH0mZnVtPUFwYWNoZSBUcmFmZmljIFNlcnZlcg==";
   char buf[1024];
   size_t length;
   bool success = true;
@@ -7684,21 +7685,50 @@ REGRESSION_TEST(SDK_API_PERCENT_ENCODING
     if (strcmp(buf, url_encoded)) {
       SDK_RPRINT(test, "TSStringPercentEncode", "TestCase1", TC_FAIL, "Failed on %s != %s",
buf, url_encoded);
       success = false;
+    } else {
+      SDK_RPRINT(test, "TSStringPercentEncode", "TestCase1", TC_PASS, "ok");
     }
   }
 
-  length = TSStringPercentDecode(url_encoded, strlen(url_encoded), buf, sizeof(buf));
-  if (length != strlen(url) || strcmp(buf, url)) {
-    SDK_RPRINT(test, "TSStringPercentDecode", "TestCase1", TC_FAIL, "Failed on %s != %s",
buf, url);
+  if (TS_SUCCESS != TSStringPercentDecode(url_encoded, strlen(url_encoded), buf, sizeof(buf),
&length)) {
+    SDK_RPRINT(test, "TSStringPercentDecode", "TestCase1", TC_FAIL, "Failed on %s", url_encoded);
     success = false;
+  } else {
+    if (length != strlen(url) || strcmp(buf, url)) {
+      SDK_RPRINT(test, "TSStringPercentDecode", "TestCase1", TC_FAIL, "Failed on %s != %s",
buf, url);
+      success = false;
+    } else {
+      SDK_RPRINT(test, "TSStringPercentDecode", "TestCase1", TC_PASS, "ok");
+    }
   }
 
-  if (success) {
-    *pstatus = REGRESSION_TEST_PASSED;
-    SDK_RPRINT(test, "TSStringPercentEncode", "TestCase1", TC_PASS, "ok");
+  if (TS_SUCCESS != TSBase64Encode(url, strlen(url), buf, sizeof(buf), &length)) {
+    SDK_RPRINT(test, "TSBase64Encode", "TestCase1", TC_FAIL, "Failed on %s", url);
+    success = false;
   } else {
-    *pstatus = REGRESSION_TEST_FAILED;
+    if (length != strlen(url_base64) || strcmp(buf, url_base64)) {
+      SDK_RPRINT(test, "TSBase64Encode", "TestCase1", TC_FAIL, "Failed on %s != %s", buf,
url_base64);
+      success = false;
+    }  else {
+      SDK_RPRINT(test, "TSBase64Encode", "TestCase1", TC_PASS, "ok");
+    }
   }
 
+  if (TS_SUCCESS != TSBase64Decode(url_base64, strlen(url_base64), (unsigned char*)buf, sizeof(buf),
&length)) {
+    SDK_RPRINT(test, "TSBase64Decode", "TestCase1", TC_FAIL, "Failed on %s", url_base64);
+    success = false;
+  } else {
+    printf("LENGTH is %d vs %d\n", (int)length, (int)strlen(url));
+    printf("BUF is %s vs %s\n", buf, url);
+    if (length != strlen(url) || strcmp(buf, url)) {
+      SDK_RPRINT(test, "TSBase64Decode", "TestCase1", TC_FAIL, "Failed on %s != %s", buf,
url);
+      success = false;
+    } else {
+      SDK_RPRINT(test, "TSBase64Decode", "TestCase1", TC_PASS, "ok");
+    }
+  }
+
+  *pstatus = success ? REGRESSION_TEST_PASSED : REGRESSION_TEST_FAILED;
+
   return;
 }

Modified: trafficserver/traffic/trunk/proxy/api/ts/ts.h.in
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/api/ts/ts.h.in?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/api/ts/ts.h.in (original)
+++ trafficserver/traffic/trunk/proxy/api/ts/ts.h.in Fri Jan  6 05:07:01 2012
@@ -1587,7 +1587,7 @@ extern "C"
      @param map optional (can be NULL) map of characters to encode.
 
   */
-  tsapi TSReturnCode TSStringPercentEncode(const char* str, int str_len, char *dst, size_t
dst_size, size_t *length, const unsigned char *map);
+  tsapi TSReturnCode TSStringPercentEncode(const char *str, int str_len, char *dst, size_t
dst_size, size_t *length, const unsigned char *map);
 
   /**
      Similar to TSStringPercentEncode(), but works on a URL object.
@@ -1608,13 +1608,14 @@ extern "C"
      in which case they overwrite. The decoded string is always 
      guaranteed to be no longer than the source string.
 
-     @param src the string to decode (and possibly write to).
-     @param src_len length of the input string (or -1).
+     @param str the string to decode (and possibly write to).
+     @param str_len length of the input string (or -1).
      @param dst output buffer (can be the same as src).
      @param dst_len size of the output buffer.
+     @param length amount of data written to the destination buffer.
 
   */
-  tsapi size_t TSStringPercentDecode(const char *src, size_t src_len, char* dst, size_t dst_size);
+  tsapi TSReturnCode TSStringPercentDecode(const char *str, size_t str_len, char *dst, size_t
dst_size, size_t *length);
 
 
 
@@ -3017,8 +3018,8 @@ extern "C"
   tsapi TSReturnCode TSHttpTxnPrivateSessionSet(TSHttpTxn txnp, int private_session);
 
   /* Expose internal Base64 Encoding / Decoding */
-  tsapi size_t TSBase64Decode(const char *input, size_t length, unsigned char *dst, size_t
dst_size);
-  tsapi size_t TSBase64Encode(const char *input, size_t length, char *dst, size_t dst_size);
+  tsapi TSReturnCode TSBase64Decode(const char *str, size_t str_len, unsigned char *dst,
size_t dst_size, size_t *length);
+  tsapi TSReturnCode TSBase64Encode(const char *str, size_t str_len, char *dst, size_t dst_size,
size_t *length);
 
 #ifdef __cplusplus
 }

Modified: trafficserver/traffic/trunk/proxy/logging/LogUtils.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/logging/LogUtils.cc?rev=1227956&r1=1227955&r2=1227956&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/logging/LogUtils.cc (original)
+++ trafficserver/traffic/trunk/proxy/logging/LogUtils.cc Fri Jan  6 05:07:01 2012
@@ -416,7 +416,7 @@ LogUtils::escapify_url(Arena *arena, cha
     }
     from++;
   }
-  *to = 0;                      // null terminate string
+  *to = '\0';                      // null terminate string
 
   *len_out = out_len;
   return new_url;



Mime
View raw message