[cpp-sp] branch main updated: Move URLEncoder in from xmltooling.

Scott Cantor cantor.2 at osu.edu
Mon Dec 2 21:04:43 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository cpp-sp.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=d82e6aacf1e8bc231ae1d1f420e7542f6d457fde

The following commit(s) were added to refs/heads/main by this push:
     new d82e6aac Move URLEncoder in from xmltooling.
d82e6aac is described below

commit d82e6aacf1e8bc231ae1d1f420e7542f6d457fde
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Dec 2 16:04:38 2024 -0500

    Move URLEncoder in from xmltooling.
---
 shibsp/util/URLEncoder.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++
 shibsp/util/URLEncoder.h   | 67 ++++++++++++++++++++++++++++++++++
 2 files changed, 157 insertions(+)

diff --git a/shibsp/util/URLEncoder.cpp b/shibsp/util/URLEncoder.cpp
new file mode 100644
index 00000000..02f101a9
--- /dev/null
+++ b/shibsp/util/URLEncoder.cpp
@@ -0,0 +1,90 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * util/URLEncoder.cpp
+ * 
+ * Interface to a URL-encoding mechanism along with a
+ * default implementation. 
+ */
+
+#include "internal.h"
+
+#include "util/URLEncoder.h"
+
+using namespace shibsp;
+using namespace std;
+
+static char x2c(char *what)
+{
+    register char digit;
+
+    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
+    digit *= 16;
+    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
+    return(digit);
+}
+
+URLEncoder::URLEncoder()
+{
+}
+
+URLEncoder::~URLEncoder()
+{
+}
+
+void URLEncoder::decode(char* s) const
+{
+    register int x,y;
+
+    for(x=0,y=0;s[y];++x,++y)
+    {
+        if((s[x] = s[y]) == '%' && isxdigit(s[y+1]) && isxdigit(s[y+2]))
+        {
+            s[x] = x2c(&s[y+1]);
+            y+=2;
+        }
+        else if (s[x] == '+')
+        {
+            s[x] = ' ';
+        }
+    }
+    s[x] = '\0';
+}
+
+static inline char hexchar(unsigned short s)
+{
+    return (s<=9) ? ('0' + s) : ('A' + s - 10);
+}
+
+string URLEncoder::encode(const char* s) const
+{
+    string ret;
+    for (; *s; s++) {
+        if (isBad(*s)) {
+            ret+='%';
+            ret+=hexchar((unsigned char)*s >> 4);
+            ret+=hexchar((unsigned char)*s & 0x0F);
+        }
+        else
+            ret+=*s;
+    }
+    return ret;
+}
+
+bool URLEncoder::isBad(char ch) const
+{
+    static char badchars[]="=&/?:\"\\+<>#%{}|^~[],`;@";
+    return (ch<=0x20 || ch>=0x7F || strchr(badchars,ch));
+}
diff --git a/shibsp/util/URLEncoder.h b/shibsp/util/URLEncoder.h
new file mode 100644
index 00000000..3e3eb720
--- /dev/null
+++ b/shibsp/util/URLEncoder.h
@@ -0,0 +1,67 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file shibsp/util/URLEncoder.h
+ *
+ * Interface to a URL-encoding mechanism along with a default implementation.
+ */
+
+#ifndef __shibsp_urlenc_h__
+#define __shibsp_urlenc_h__
+
+#include <shibsp/base.h>
+
+namespace shibsp {
+    /**
+     * Interface to a URL-encoding mechanism along with a default implementation.
+     *
+     * Since URL-encoding is not canonical, it's important that the same
+     * encoder is used during some library operations and the calling code.
+     */
+    class XMLTOOL_API URLEncoder {
+        MAKE_NONCOPYABLE(URLEncoder);
+    public:
+        URLEncoder();
+
+        virtual ~URLEncoder();
+
+        /**
+         * Produce a URL-safe but equivalent version of the input string.
+         *
+         * @param s input string to encode
+         * @return a string object containing the result of encoding the input
+         */
+        virtual std::string encode(const char* s) const;
+
+        /**
+         * Perform an in-place decoding operation on the input string.
+         * The resulting string will be NULL-terminated.
+         *
+         * @param s input string to decode in a writable buffer
+         */
+        virtual void decode(char* s) const;
+
+    protected:
+        /**
+         * Returns true iff the input character requires encoding.
+         *
+         * @param ch    the character to check
+         * @return  true iff the character should be encoded
+         */
+        virtual bool isBad(char ch) const;
+    };
+};
+
+#endif /* __shibsp_urlenc_h__ */

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list