← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba

 

Paul J. Lucas has proposed merging lp:~paul-lucas/zorba/pjl-misc into lp:zorba.

Commit message:
Replaced public Base64 API with internal one that's much better.

Requested reviews:
  Paul J. Lucas (paul-lucas)

For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/177668

Replaced public Base64 API with internal one that's much better.

https://code.launchpad.net/~zorba-coders/zorba/new-base64-api/+merge/177667
-- 
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/177668
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'doc/cxx/examples/binary.cpp'
--- doc/cxx/examples/binary.cpp	2013-06-12 04:55:14 +0000
+++ doc/cxx/examples/binary.cpp	2013-07-30 18:41:28 +0000
@@ -23,16 +23,18 @@
 bool 
 encode_example() 
 {
-  String lString("Hello Zorba");
-  String lEncoded = zorba::base64::encode(lString);
+  String const lString("Hello Zorba");
+  String lEncoded;
+  base64::encode(lString, &lEncoded);
   return lEncoded == "SGVsbG8gWm9yYmE="; 
 }
 
 bool 
 decode_example()
 {
-  String lEncoded("SGVsbG8gWm9yYmE=");
-  String lDecoded = zorba::base64::decode(lEncoded);
+  String const lEncoded("SGVsbG8gWm9yYmE=");
+  String lDecoded;
+  base64::decode(lEncoded, &lDecoded);
   return lDecoded == "Hello Zorba";
 }
 

=== modified file 'include/zorba/util/base64_util.h'
--- include/zorba/util/base64_util.h	2013-06-12 04:55:14 +0000
+++ include/zorba/util/base64_util.h	2013-07-30 18:41:28 +0000
@@ -1,12 +1,12 @@
 /*
- * Copyright 2006-2009 The FLWOR Foundation.
- *
+ * Copyright 2006-2008 The FLWOR Foundation.
+ * 
  * 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.
@@ -14,32 +14,436 @@
  * limitations under the License.
  */
 
+#pragma once
 #ifndef ZORBA_BASE64_API_H
 #define ZORBA_BASE64_API_H
 
 // standard
 #include <iostream>
+#include <stdexcept>
+#include <sys/types.h>                  /* for size_t */
+#include <vector>
 
-// zorba
+// Zorba
 #include <zorba/config.h>
-#include <zorba/zorba_string.h>
+#include <zorba/internal/cxx_util.h>
+#include <zorba/internal/ztd.h>
+#include <zorba/util/stream_util.h>
 
 namespace zorba {
 namespace base64 {
 
-///////////////////////////////////////////////////////////////////////////////
-
-ZORBA_DLL_PUBLIC
-String encode( String const &aString );
-
-ZORBA_DLL_PUBLIC
-String encode( std::istream &aStream );
-
-ZORBA_DLL_PUBLIC
-String decode( String const &aString );
-
-ZORBA_DLL_PUBLIC
-String decode( std::istream &aStream );  
+////////// Types //////////////////////////////////////////////////////////////
+
+typedef size_t size_type;
+
+/**
+ * Options to use for decoding.
+ */
+enum decode_options {
+  dopt_none       = 0x00, ///< No options.
+  dopt_any_len    = 0x01, ///< Input length may be non-multiple of 4.
+  dopt_ignore_ws  = 0x02, ///< Ignore all whitespace.
+};
+
+////////// Exception //////////////////////////////////////////////////////////
+
+/**
+ * A %base64::exception is-an invalid_argument that contains additional details
+ * about the exception such as the invalid character and its offset.
+ */
+class exception : public std::invalid_argument {
+public:
+  exception( char c, size_type offset, std::string const &msg ) :
+    std::invalid_argument( msg ), char_( c ), offset_( offset ) { }
+
+  char invalid_char() const {
+    return char_;
+  }
+
+  size_type char_offset() const {
+    return offset_;
+  }
+
+private:
+  char char_;
+  size_type offset_;
+};
+
+////////// Decoding ///////////////////////////////////////////////////////////
+
+/**
+ * Calculates the number of bytes required to decode \a n Base64-encoded bytes.
+ *
+ * @param n The number of bytes to decode.
+ * @return Returns the number of bytes needed for Base64 decoding.
+ */
+inline size_type decoded_size( size_type n ) {
+  return ((n / 4) + !!(n % 4)) * 3;
+}
+
+/**
+ * Decodes a Base64-encoded buffer.  Embedded newlines and carriage-returns are
+ * skipped.
+ *
+ * @param from A pointer to the Base64 buffer to be decoded.
+ * @param from_len The number of bytes to decode.
+ * @paran to A pointer to the buffer to receive the decoded bytes.  The buffer
+ * must be large enough to contain them.  Note that the buffer is \e not null
+ * terminated.
+ * @param options The decoding options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dtop_any_len bit
+ * set and \a from_len is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ * @see decoded_size()
+ */
+ZORBA_DLL_PUBLIC
+size_type decode( char const *from, size_type from_len, char *to,
+                  int options = dopt_none );
+
+/**
+ * Decodes a Base64-encoded buffer and appends the decoded bytes onto a
+ * vector&lt;char&gt;.  Embedded newlines and carriage-returns are skipped.
+ *
+ * @param from A pointer to the buffer to be encoded.
+ * @param from_len The number of bytes to encode.
+ * @param to A pointer to the vector to append the encoded bytes appended onto.
+ * The vector is made large enough to contain the additional bytes.
+ * @param options The decoding options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+ZORBA_DLL_PUBLIC
+size_type decode( char const *from, size_type from_len, std::vector<char> *to,
+                  int options = dopt_none );
+
+/**
+ * Base64-decodes a buffer and writes the decoded bytes to the given stream.
+ *
+ * @param from A pointer to the Base64 buffer to be decoded.
+ * @param from_len The number of bytes to decode.
+ * @param to The ostream to write the decoded bytes to.
+ * @param options The options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+ZORBA_DLL_PUBLIC
+size_type decode( char const *from, size_type from_len, std::ostream &to,
+                  int options = dopt_none );
+
+/**
+ * Decodes a Base64-encoded buffer and appends the decoded bytes onto a string.
+ * Embedded newlines and carriage-returns are skipped.
+ *
+ * @tparam ToStringType The string type.
+ * @param from A pointer to the Base64 buffer to be decoded.
+ * @param from_len The number of bytes to decode.
+ * @param to The string to append the decoded bytes to.
+ * @param options The options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+template<class ToStringType>
+typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
+decode( char const *from, size_type from_len, ToStringType *to,
+        int options = dopt_none ) {
+  size_type decoded = 0;
+  if ( from_len ) {
+    typename ToStringType::size_type const orig_size = to->size();
+    to->resize( orig_size + decoded_size( from_len ) );
+    decoded = decode( from, from_len, &to->at( orig_size ), options );
+    to->resize( orig_size + decoded );
+  }
+  return decoded;
+}
+
+/**
+ * Decodes a Base64-encoded string and appends the decoded bytes onto another
+ * string.  Embedded newlines and carriage-returns are skipped.
+ *
+ * @tparam FromStringType The \a from string type.
+ * @tparam ToStringType The \a to string type.
+ * @param from The Base64-encoded string to be decoded.
+ * @param to The string to append the decoded bytes to.
+ * @param options The options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+template<class FromStringType,class ToStringType> inline
+typename std::enable_if<ZORBA_IS_STRING(FromStringType)
+                     && ZORBA_IS_STRING(ToStringType),
+                        size_type>::type
+decode( FromStringType const &from, ToStringType *to ) {
+  return decode( from.data(), from.size(), to );
+}
+
+/**
+ * Decodes a Base64-encoded istream.  Embedded newlines and carriage-returns
+ * are skipped.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The ostream to write the decoded bytes to.
+ * @param options The options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+ZORBA_DLL_PUBLIC
+size_type decode( std::istream &from, std::ostream &to,
+                  int options = dopt_none );
+
+/**
+ * Base64-decodes a string and writes the decoded bytes to a stream.
+ *
+ * @tparam FromStringType The string type.
+ * @param from The string to be decoded.
+ * @param to The ostream to write the decoded bytes to.
+ * @return Returns the number of decoded bytes.
+ */
+template<class FromStringType> inline
+typename std::enable_if<ZORBA_IS_STRING(FromStringType),size_type>::type
+decode( FromStringType const &from, std::ostream &to ) {
+  return encode( from.data(), from.size(), to );
+}
+
+/**
+ * Decodes a Base64-encoded istream and appends the decoded bytes to a string.
+ * Embedded newlines and carriage-returns are skipped.
+ *
+ * @tparam ToStringType The string type.
+ * @param from The istream to read from until EOF is reached.
+ * @param to The string to append the decoded bytes to.
+ * @param options The options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+template<class ToStringType>
+typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
+decode( std::istream &from, ToStringType *to, int options = dopt_none ) {
+  bool const ignore_ws = !!(options & dopt_ignore_ws);
+  size_type total_decoded = 0;
+  while ( !from.eof() ) {
+    char from_buf[ 1024 * 4 ], to_buf[ 1024 * 3 ];
+    std::streamsize gcount;
+    if ( ignore_ws )
+      gcount = read_without_whitespace( from, from_buf, sizeof from_buf );
+    else {
+      from.read( from_buf, sizeof from_buf );
+      gcount = from.gcount();
+    }
+    if ( gcount ) {
+      size_type const decoded =
+        decode( from_buf, static_cast<size_type>( gcount ), to_buf, options );
+      to->append( to_buf, decoded );
+      total_decoded += decoded;
+    } else
+      break;
+  }
+  return total_decoded;
+}
+
+/**
+ * Decodes a Base64-encoded stream and appends the decoded bytes onto a
+ * vector&lt;char;&gt;.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The string to append the decoded bytes to.
+ * @param options The options to use.
+ * @param Returns the number of decoded bytes.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes decoded is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ */
+ZORBA_DLL_PUBLIC
+size_type decode( std::istream &from, std::vector<char> *to,
+                  int options = dopt_none );
+
+/**
+ * Validates a Base64-encoded buffer.  Embedded newlines and carriage-returns
+ * are skipped.
+ *
+ * @param buf A pointer to the Base64 buffer to be validated.
+ * @param buf_len The number of bytes to validate.
+ * @param options The options to use.
+ * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
+ * set and the number of Base64 bytes validated is not a multiple of 4.
+ * @throws base64::exception if an \c = is encountered unexpectedly or an
+ * invalid byte is encountered.
+ * @see decoded_size()
+ */
+inline void validate( char const *buf, size_type buf_len,
+                      int options = dopt_none ) {
+  decode( buf, buf_len, static_cast<char*>( nullptr ), options );
+}
+
+////////// Encoding ///////////////////////////////////////////////////////////
+
+/**
+ * Calculates the number of bytes required to Base64-encode \a n bytes.
+ *
+ * @param n The number of bytes to encode.
+ * @return Returns the number of bytes needed for Base64 encoding.
+ */
+inline size_type encoded_size( size_type n ) {
+  return (n + 2) / 3 * 4;
+}
+
+/**
+ * Base64-encodes a buffer.
+ *
+ * @param from A pointer to the buffer to be encoded.
+ * @param from_len The number of bytes to encode.
+ * @param to A pointer to the buffer to receive the encoded bytes.  The buffer
+ * must be large enough to contain them.  Note that the buffer is \e not null
+ * terminated.
+ * @return Returns the number of encoded bytes.
+ * @see encoded_size()
+ */
+ZORBA_DLL_PUBLIC
+size_type encode( char const *from, size_type from_len, char *to );
+
+/**
+ * Base64-encodes a buffer and appends the encoded bytes onto a
+ * vector&lt;char&gt;.
+ *
+ * @param from A pointer to the buffer to be encoded.
+ * @param from_len The number of bytes to encode.
+ * @param to A pointer to the vector to append the encoded bytes appended onto.
+ * The vector is made large enough to contain the additional bytes.
+ */
+ZORBA_DLL_PUBLIC
+size_type encode( char const *from, size_type from_len, std::vector<char> *to );
+
+/**
+ * Base64-encodes a buffer and writes the encoded bytes to the given stream.
+ *
+ * @param from A pointer to the Base64 buffer to be encoded.
+ * @param from_len The number of bytes to encode.
+ * @param to The ostream to write the encoded bytes to.
+ * @return Returns the number of encoded bytes.
+ */
+ZORBA_DLL_PUBLIC
+size_type encode( char const *from, size_type from_len, std::ostream &to );
+
+/**
+ * Base64-encodes a buffer and appends the encoded bytes onto a string.
+ *
+ * @tparam ToStringType The string type.
+ * @param from A pointer to the Base64 buffer to be encoded.
+ * @param from_len The number of bytes to encode.
+ * @param to A pointer to the string to append the encoded bytes onto.
+ * @return Returns the number of encoded bytes.
+ */
+template<class ToStringType>
+typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
+encode( char const *from, size_type from_len, ToStringType *to ) {
+  size_type encoded = 0;
+  if ( from_len ) {
+    typename ToStringType::size_type const orig_size = to->size();
+    to->resize( orig_size + encoded_size( from_len ) );
+    encoded = encode( from, from_len, &to->at( orig_size ) );
+    to->resize( orig_size + encoded );
+  }
+  return encoded;
+}
+
+/**
+ * Base64-encodes a string and appends the encoded bytes onto another string.
+ *
+ * @tparam FromStringType The \a from string type.
+ * @tparam ToStringType The \a to string type.
+ * @param from The string to be encoded.
+ * @param to A pointer to the string to append the encoded bytes onto.
+ * @return Returns the number of encoded bytes.
+ */
+template<class FromStringType,class ToStringType> inline
+typename std::enable_if<ZORBA_IS_STRING(FromStringType)
+                     && ZORBA_IS_STRING(ToStringType),
+                        size_type>::type
+encode( FromStringType const &from, ToStringType *to ) {
+  return encode( from.data(), from.size(), to );
+}
+
+/**
+ * Base64-encodes one stream and writes the encoded bytes to another.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The ostream to write the encoded bytes to.
+ * @return Returns the number of encoded bytes.
+ */
+ZORBA_DLL_PUBLIC
+size_type encode( std::istream &from, std::ostream &to );
+
+/**
+ * Base64-encodes a string and writes the encoded bytes to a stream.
+ *
+ * @tparam FromStringType The string type.
+ * @param from The string to be encoded.
+ * @param to The ostream to write the encoded bytes to.
+ * @return Returns the number of encoded bytes.
+ */
+template<class FromStringType> inline
+typename std::enable_if<ZORBA_IS_STRING(FromStringType),size_type>::type
+encode( FromStringType const &from, std::ostream &to ) {
+  return encode( from.data(), from.size(), to );
+}
+
+/**
+ * Encodes a stream to Base64 and appends the encoded bytes to a string.
+ *
+ * @tparam ToStringType The string type.
+ * @param from The istream to read from until EOF is reached.
+ * @param to The string to append the encoded bytes to.
+ * @return Returns the number of encoded bytes.
+ */
+template<class ToStringType>
+typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
+encode( std::istream &from, ToStringType *to ) {
+  size_type total_encoded = 0;
+  while ( !from.eof() ) {
+    char from_buf[ 1024 * 3 ], to_buf[ 1024 * 4 ];
+    from.read( from_buf, sizeof from_buf );
+    if ( std::streamsize const gcount = from.gcount() ) {
+      size_type const encoded =
+        encode( from_buf, static_cast<size_type>( gcount ), to_buf );
+      to->append( to_buf, encoded );
+      total_encoded += encoded;
+    } else
+      break;
+  }
+  return total_encoded;
+}
+
+/**
+ * Base64-encodes a stream and appends the encoded bytes onto a
+ * vector&lt;char;&gt;.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The vector to append the encoded bytes to.
+ * @param Returns the number of encoded bytes.
+ */
+ZORBA_DLL_PUBLIC
+size_type encode( std::istream &from, std::vector<char> *to );
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -47,4 +451,9 @@
 } // namespace zorba
 
 #endif /* ZORBA_BASE64_API_H */
+/*
+ * Local variables:
+ * mode: c++
+ * End:
+ */
 /* vim:set et sw=2 ts=2: */

=== modified file 'include/zorba/util/stream_util.h'
--- include/zorba/util/stream_util.h	2013-06-05 04:13:37 +0000
+++ include/zorba/util/stream_util.h	2013-07-30 18:41:28 +0000
@@ -35,6 +35,19 @@
 ZORBA_DLL_PUBLIC
 bool skip_utf8_bom( std::istream &is );
 
+/**
+ * Reads from the given istream until \a n non-whitespace characters are read
+ * or until EOF is encountered.
+ *
+ * @param is The istream to read from.
+ * @param buf A pointer to the start of a buffer to read into.
+ * @param n The number of non-whitespace characters to read.
+ * @return Returns the number of non-whitespace characters read.
+ */
+ZORBA_DLL_PUBLIC
+std::streamsize read_without_whitespace( std::istream &is, char *buf,
+                                         std::streamsize n );
+
 ///////////////////////////////////////////////////////////////////////////////
 
 } // namespace zorba

=== modified file 'modules/http-client/json/http-client.xq.src/http_request_handler.cpp'
--- modules/http-client/json/http-client.xq.src/http_request_handler.cpp	2013-07-19 17:36:52 +0000
+++ modules/http-client/json/http-client.xq.src/http_request_handler.cpp	2013-07-30 18:41:28 +0000
@@ -141,7 +141,7 @@
         {
           lAuth += aAuthMethod + " ";
         }
-        lAuth += zorba::base64::encode(lAuthString);
+        zorba::base64::encode(lAuthString, &lAuth);
         theAuthMethod = lAuth.c_str();
         theHeaderLists[0] = curl_slist_append(theHeaderLists[0], theAuthMethod.c_str());
       }
@@ -318,9 +318,7 @@
     const char * lData = aItem.getBase64BinaryValue(lLen);
     if (aItem.isEncoded())
     {
-      String lEncoded(lData,lLen);
-      String lDecodedData = zorba::base64::decode(lEncoded);
-      *theSerStream << lDecodedData;
+      zorba::base64::decode(lData, lLen, *theSerStream);
     }
     else
     {

=== modified file 'modules/http-client/json/http-client.xq.src/http_response_parser.cpp'
--- modules/http-client/json/http-client.xq.src/http_response_parser.cpp	2013-07-19 17:36:52 +0000
+++ modules/http-client/json/http-client.xq.src/http_response_parser.cpp	2013-07-30 18:41:28 +0000
@@ -339,7 +339,8 @@
     // TODO: once a proper streaming implementation is in place this can be
     // changed. This required a Base64 encoding stream since the item factory
     // work only builds base64binary and assumes the data is already encoded.
-    String lEncoded = zorba::base64::encode(aStream);
+    String lEncoded;
+    zorba::base64::encode(aStream, &lEncoded);
     return lFactory->createBase64Binary(lEncoded.data(), lEncoded.size(), true);
   }
 

=== modified file 'modules/org/expath/ns/file.xq.src/file.cpp'
--- modules/org/expath/ns/file.xq.src/file.cpp	2013-06-13 18:12:45 +0000
+++ modules/org/expath/ns/file.xq.src/file.cpp	2013-07-30 18:41:28 +0000
@@ -28,7 +28,6 @@
 #include <zorba/serializer.h>
 #include <zorba/singleton_item_sequence.h>
 #include <zorba/user_exception.h>
-#include <zorba/util/base64_util.h>
 #include <zorba/util/fs_util.h>
 #include <zorba/util/stream_util.h>
 #include <zorba/util/transcode_stream.h>

=== modified file 'src/api/CMakeLists.txt'
--- src/api/CMakeLists.txt	2013-06-21 06:20:46 +0000
+++ src/api/CMakeLists.txt	2013-07-30 18:41:28 +0000
@@ -15,6 +15,7 @@
 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/api/version.cpp.cmake ${CMAKE_CURRENT_BINARY_DIR}/api/version.cpp)
 
 SET(API_SRCS
+    base64_util.cpp
     smart_ptr.cpp
     diagnostic_handler.cpp
     zorba.cpp
@@ -49,7 +50,6 @@
     item_sequence_chainer.cpp
     empty_sequence.cpp
     serializerimpl.cpp
-    base64impl.cpp
     base64_streambuf.cpp
     uriimpl.cpp
     uriresolverimpl.cpp

=== modified file 'src/api/base64_streambuf.cpp'
--- src/api/base64_streambuf.cpp	2013-06-20 00:52:58 +0000
+++ src/api/base64_streambuf.cpp	2013-07-30 18:41:28 +0000
@@ -16,16 +16,16 @@
 
 #include "stdafx.h"
 
+// standard
 #include <stdexcept>
-
 //#define ZORBA_DEBUG_BASE64_STREAMBUF
 #ifdef ZORBA_DEBUG_BASE64_STREAMBUF
 # include <stdio.h>
 #endif
 
+// Zorba
 #include <zorba/util/base64_stream.h>
-
-#include "util/base64_util.h"
+#include <zorba/util/base64_util.h>
 
 using namespace std;
 

=== renamed file 'src/util/base64_util.cpp' => 'src/api/base64_util.cpp'
--- src/util/base64_util.cpp	2013-04-16 22:12:03 +0000
+++ src/api/base64_util.cpp	2013-07-30 18:41:28 +0000
@@ -20,10 +20,11 @@
 #include <algorithm>
 #include <cstring>
 
-// local
-#include "ascii_util.h"
-#include "base64_util.h"
-#include "string_util.h"
+// Zorba
+#include <zorba/util/base64_util.h>
+#include "util/ascii_util.h"
+#include "util/mem_streambuf.h"
+#include "util/string_util.h"
 
 using namespace std;
 
@@ -217,6 +218,14 @@
   return decoded;
 }
 
+size_type decode( char const *from, size_type from_len, ostream &to,
+                  int options ) {
+  mem_streambuf buf( const_cast<char*>( from ), from_len );
+  istringstream iss;
+  iss.ios::rdbuf( &buf );
+  return decode( iss, to, options );
+}
+
 size_type decode( istream &from, ostream &to, int options ) {
   bool const ignore_ws = !!(options & dopt_ignore_ws);
   size_type total_decoded = 0;
@@ -319,6 +328,13 @@
   return encoded;
 }
 
+size_type encode( char const *from, size_type from_len, ostream &to ) {
+  mem_streambuf buf( const_cast<char*>( from ), from_len );
+  istringstream iss;
+  iss.ios::rdbuf( &buf );
+  return encode( iss, to );
+}
+
 size_type encode( istream &from, ostream &to ) {
   size_type total_encoded = 0;
   while ( !from.eof() ) {

=== removed file 'src/api/base64impl.cpp'
--- src/api/base64impl.cpp	2013-06-12 04:55:14 +0000
+++ src/api/base64impl.cpp	1970-01-01 00:00:00 +0000
@@ -1,84 +0,0 @@
-/*
- * Copyright 2006-2009 The FLWOR Foundation.
- *
- * 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.
- */
-#include "stdafx.h"
-
-#include <sstream> 
-#include <zorba/diagnostic_list.h>
-#include <zorba/util/base64_util.h>
-
-#include <zorba/config.h>
-#include <zorba/zorba_string.h>
-
-#include "diagnostics/dict.h"
-#include "diagnostics/xquery_exception.h"
-#include "util/base64_util.h"
-
-#define CATCH_BASE64_EXCEPTION()                                        \
-  catch (base64::exception const &e)                                    \
-  {                                                                     \
-    throw XQUERY_EXCEPTION(err::FORG0001,                               \
-    ERROR_PARAMS(ZED(FORG0001_Base64BadChar_2),  e.invalid_char()));    \
-  }                                                                     \
-  catch (std::invalid_argument const&)                                  \
-  {                                                                     \
-    throw XQUERY_EXCEPTION(err::FORG0001,                               \
-    ERROR_PARAMS(ZED(FORG0001_Base64Multiple4)));                       \
-  }
-
-using namespace std;
-
-namespace zorba {
-namespace base64 {
-
-///////////////////////////////////////////////////////////////////////////////
-
-String encode(String const &aString ) {
-  String result;
-  encode( aString.data(), aString.size(), &result );
-  return result;
-}
-
-String encode( istream& aStream ) {
-  String result;
-  encode( aStream, &result );
-  return result;
-}
-
-
-String decode( String const &aString ) {
-  try {
-    String result;
-    decode( aString.data(), aString.size(), &result, dopt_ignore_ws );
-    return result;
-  }
-  CATCH_BASE64_EXCEPTION()
-}
-
-
-String decode( istream &aStream ) {
-  try {
-    String result;
-    decode( aStream, &result, dopt_ignore_ws );
-    return result;
-  }
-  CATCH_BASE64_EXCEPTION()
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-} // namespace base64
-} // namespace zorba
-/* vim:set et sw=2 ts=2: */

=== modified file 'src/debugger/debugger_clientimpl.cpp'
--- src/debugger/debugger_clientimpl.cpp	2013-06-12 04:55:14 +0000
+++ src/debugger/debugger_clientimpl.cpp	2013-07-30 18:41:28 +0000
@@ -569,7 +569,9 @@
 DebuggerClientImpl::eval(std::string const &aExpr)
 {
   std::size_t id = ++theLastId;
-  *theOutStream << "eval -i " << id << " -- " << base64::encode(aExpr.c_str()) << '\0';
+  *theOutStream << "eval -i " << id << " -- ";
+  base64::encode( aExpr.data(), aExpr.size(), *theOutStream );
+  *theOutStream << '\0';
   theOutStream->flush();
   return id;
 }

=== modified file 'src/debugger/debugger_server.cpp'
--- src/debugger/debugger_server.cpp	2013-06-12 04:55:14 +0000
+++ src/debugger/debugger_server.cpp	2013-07-30 18:41:28 +0000
@@ -400,8 +400,9 @@
 
         try {
 
-          String lEncodedData(aCommand.getData());
-          String lDecodedData = base64::decode(lEncodedData);
+          String const lEncodedData(aCommand.getData());
+          String lDecodedData;
+          base64::decode( lEncodedData, &lDecodedData );
 
           zstring lVar(lDecodedData.c_str());
           std::list<std::pair<zstring, zstring> > lResults = theRuntime->eval(lVar);
@@ -733,8 +734,8 @@
   if (lFetchChildren && lSize > 1) {
     buildChildProperties(aName, lResults, aStream);
   } else if (lResults.size() == 1) {
-    String lValue(lResults.front().first.c_str());
-    aStream << base64::encode(lValue);
+    String const lValue(lResults.front().first.c_str());
+    base64::encode( lValue.data(), lValue.size(), aStream );
   }
 
   aStream << "</property>";
@@ -760,9 +761,9 @@
       << "type=\"" << lIter->second << "\" "
       << "encoding=\"base64\" "
       << "constant=\"1\" "
-      << "children=\"0\" "
-      << ">" << base64::encode(lValue)
-      << "</property>";
+      << "children=\"0\">";
+    base64::encode( lValue, aStream );
+    aStream << "</property>";
   }
 }
 
@@ -817,4 +818,5 @@
   );
 }
 
-} /* namespace zorba */
+} // namespace zorba
+/* vim:set et sw=2 ts=2: */

=== modified file 'src/store/naive/atomic_items.cpp'
--- src/store/naive/atomic_items.cpp	2013-07-01 18:59:06 +0000
+++ src/store/naive/atomic_items.cpp	2013-07-30 18:41:28 +0000
@@ -20,6 +20,7 @@
 #include <limits.h>
 
 #include <zorba/internal/unique_ptr.h>
+#include <zorba/util/base64_util.h>
 
 #include "diagnostics/assert.h"
 #include "diagnostics/xquery_diagnostics.h"
@@ -45,7 +46,6 @@
 #include "tree_id.h"
 
 #include "util/ascii_util.h"
-#include "util/base64_util.h"
 #include "util/mem_sizeof.h"
 #include "util/string_util.h"
 #include "util/utf8_util.h"

=== modified file 'src/unit_tests/test_base64.cpp'
--- src/unit_tests/test_base64.cpp	2013-02-07 17:24:36 +0000
+++ src/unit_tests/test_base64.cpp	2013-07-30 18:41:28 +0000
@@ -21,7 +21,7 @@
 #include <stdexcept>
 #include <string>
 
-#include "util/base64_util.h"
+#include <zorba/util/base64_util.h>
 
 using namespace std;
 using namespace zorba;

=== modified file 'src/util/CMakeLists.txt'
--- src/util/CMakeLists.txt	2013-06-12 00:21:05 +0000
+++ src/util/CMakeLists.txt	2013-07-30 18:41:28 +0000
@@ -14,7 +14,6 @@
 
 SET(UTIL_SRCS
   ascii_util.cpp
-  base64_util.cpp
   dynamic_bitset.cpp
   error_util.cpp
   fs_util.cpp

=== removed file 'src/util/base64_util.h'
--- src/util/base64_util.h	2013-06-01 00:30:39 +0000
+++ src/util/base64_util.h	1970-01-01 00:00:00 +0000
@@ -1,353 +0,0 @@
-/*
- * Copyright 2006-2008 The FLWOR Foundation.
- * 
- * 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.
- */
-
-#pragma once
-#ifndef ZORBA_BASE64_UTIL_H
-#define ZORBA_BASE64_UTIL_H
-
-// standard
-#include <iostream>
-#include <stdexcept>
-#include <sys/types.h>                  /* for size_t */
-#include <vector>
-
-// Zorba
-#include <zorba/internal/cxx_util.h>
-#include <zorba/internal/ztd.h>
-#include "stream_util.h"
-
-namespace zorba {
-namespace base64 {
-
-////////// Types //////////////////////////////////////////////////////////////
-
-typedef size_t size_type;
-
-/**
- * Options to use for decoding.
- */
-enum decode_options {
-  dopt_none       = 0x00, ///< No options.
-  dopt_any_len    = 0x01, ///< Input length may be non-multiple of 4.
-  dopt_ignore_ws  = 0x02, ///< Ignore all whitespace.
-};
-
-////////// Exception //////////////////////////////////////////////////////////
-
-/**
- * A %base64::exception is-an invalid_argument that contains additional details
- * about the exception such as the invalid character and its offset.
- */
-class exception : public std::invalid_argument {
-public:
-  exception( char c, size_type offset, std::string const &msg ) :
-    std::invalid_argument( msg ), char_( c ), offset_( offset ) { }
-
-  char invalid_char() const {
-    return char_;
-  }
-
-  size_type char_offset() const {
-    return offset_;
-  }
-
-private:
-  char char_;
-  size_type offset_;
-};
-
-////////// Decoding ///////////////////////////////////////////////////////////
-
-/**
- * Calculates the number of bytes required to decode \a n Base64-encoded bytes.
- *
- * @param n The number of bytes to decode.
- * @return Returns the number of bytes needed for Base64 decoding.
- */
-inline size_type decoded_size( size_type n ) {
-  return ((n / 4) + !!(n % 4)) * 3;
-}
-
-/**
- * Decodes a Base64-encoded buffer.  Embedded newlines and carriage-returns are
- * skipped.
- *
- * @param from A pointer to the Base64 buffer to be decoded.
- * @param from_len The number of bytes to decode.
- * @paran to A pointer to the buffer to receive the decoded bytes.  The buffer
- * must be large enough to contain them.  Note that the buffer is \e not null
- * terminated.
- * @param options The decoding options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dtop_any_len bit
- * set and \a from_len is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- * @see decoded_size()
- */
-size_type decode( char const *from, size_type from_len, char *to,
-                  int options = dopt_none );
-
-/**
- * Decodes a Base64-encoded buffer and appends the decoded bytes onto a
- * vector&lt;char&gt;.  Embedded newlines and carriage-returns are skipped.
- *
- * @param from A pointer to the buffer to be encoded.
- * @param from_len The number of bytes to encode.
- * @param to A pointer to the vector to append the encoded bytes appended onto.
- * The vector is made large enough to contain the additional bytes.
- * @param options The decoding options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes decoded is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- */
-size_type decode( char const *from, size_type from_len, std::vector<char> *to,
-                  int options = dopt_none );
-
-/**
- * Decodes a Base64-encoded buffer and appends the decoded bytes onto a string.
- * Embedded newlines and carriage-returns are skipped.
- *
- * @tparam ToStringType The string type.
- * @param from A pointer to the Base64 buffer to be decoded.
- * @param from_len The number of bytes to decode.
- * @param to The string to append the decoded bytes to.
- * @param options The options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes decoded is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- */
-template<class ToStringType>
-typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
-decode( char const *from, size_type from_len, ToStringType *to,
-        int options = dopt_none ) {
-  size_type decoded = 0;
-  if ( from_len ) {
-    typename ToStringType::size_type const orig_size = to->size();
-    to->resize( orig_size + decoded_size( from_len ) );
-    decoded = decode( from, from_len, &to->at( orig_size ), options );
-    to->resize( orig_size + decoded );
-  }
-  return decoded;
-}
-
-/**
- * Decodes a Base64-encoded istream.  Embedded newlines and carriage-returns
- * are skipped.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The ostream to write the decoded bytes to.
- * @param options The options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes decoded is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- */
-size_type decode( std::istream &from, std::ostream &to,
-                  int options = dopt_none );
-
-/**
- * Decodes a Base64-encoded istream and appends the decoded bytes to a string.
- * Embedded newlines and carriage-returns are skipped.
- *
- * @tparam ToStringType The string type.
- * @param from The istream to read from until EOF is reached.
- * @param to The string to append the decoded bytes to.
- * @param options The options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes decoded is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- */
-template<class ToStringType>
-typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
-decode( std::istream &from, ToStringType *to, int options = dopt_none ) {
-  bool const ignore_ws = !!(options & dopt_ignore_ws);
-  size_type total_decoded = 0;
-  while ( !from.eof() ) {
-    char from_buf[ 1024 * 4 ], to_buf[ 1024 * 3 ];
-    std::streamsize gcount;
-    if ( ignore_ws )
-      gcount = read_without_whitespace( from, from_buf, sizeof from_buf );
-    else {
-      from.read( from_buf, sizeof from_buf );
-      gcount = from.gcount();
-    }
-    if ( gcount ) {
-      size_type const decoded =
-        decode( from_buf, static_cast<size_type>( gcount ), to_buf, options );
-      to->append( to_buf, decoded );
-      total_decoded += decoded;
-    } else
-      break;
-  }
-  return total_decoded;
-}
-
-/**
- * Decodes a Base64-encoded stream and appends the decoded bytes onto a
- * vector&lt;char;&gt;.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The string to append the decoded bytes to.
- * @param options The options to use.
- * @param Returns the number of decoded bytes.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes decoded is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- */
-size_type decode( std::istream &from, std::vector<char> *to,
-                  int options = dopt_none );
-
-/**
- * Validates a Base64-encoded buffer.  Embedded newlines and carriage-returns
- * are skipped.
- *
- * @param buf A pointer to the Base64 buffer to be validated.
- * @param buf_len The number of bytes to validate.
- * @param options The options to use.
- * @throws invalid_argument if \a options does not have the \c dopt_any_len bit
- * set and the number of Base64 bytes validated is not a multiple of 4.
- * @throws base64::exception if an \c = is encountered unexpectedly or an
- * invalid byte is encountered.
- * @see decoded_size()
- */
-inline void validate( char const *buf, size_type buf_len,
-                      int options = dopt_none ) {
-  decode( buf, buf_len, static_cast<char*>( nullptr ), options );
-}
-
-////////// Encoding ///////////////////////////////////////////////////////////
-
-/**
- * Calculates the number of bytes required to Base64-encode \a n bytes.
- *
- * @param n The number of bytes to encode.
- * @return Returns the number of bytes needed for Base64 encoding.
- */
-inline size_type encoded_size( size_type n ) {
-  return (n + 2) / 3 * 4;
-}
-
-/**
- * Base64-encodes a buffer.
- *
- * @param from A pointer to the buffer to be encoded.
- * @param from_len The number of bytes to encode.
- * @param to A pointer to the buffer to receive the encoded bytes.  The buffer
- * must be large enough to contain them.  Note that the buffer is \e not null
- * terminated.
- * @return Returns the number of encoded bytes.
- * @see encoded_size()
- */
-size_type encode( char const *from, size_type from_len, char *to );
-
-/**
- * Base64-encodes a buffer and appends the encoded bytes onto a
- * vector&lt;char&gt;.
- *
- * @param from A pointer to the buffer to be encoded.
- * @param from_len The number of bytes to encode.
- * @param to A pointer to the vector to append the encoded bytes appended onto.
- * The vector is made large enough to contain the additional bytes.
- */
-size_type encode( char const *from, size_type from_len, std::vector<char> *to );
-
-/**
- * Base64-encodes a buffer and appends the encoded bytes onto a string.
- *
- * @tparam ToStringType The string type.
- * @param from A pointer to the Base64 buffer to be encoded.
- * @param from_len The number of bytes to encode.
- * @param to A pointer to the string to append the encoded bytes onto.
- * @return Returns the number of encoded bytes.
- */
-template<class ToStringType>
-typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
-encode( char const *from, size_type from_len, ToStringType *to ) {
-  size_type encoded = 0;
-  if ( from_len ) {
-    typename ToStringType::size_type const orig_size = to->size();
-    to->resize( orig_size + encoded_size( from_len ) );
-    encoded = encode( from, from_len, &to->at( orig_size ) );
-    to->resize( orig_size + encoded );
-  }
-  return encoded;
-}
-
-/**
- * Base64-encodes one stream and write the encoded bytes to another.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The ostream to write the encoded bytes to.
- */
-size_type encode( std::istream &from, std::ostream &to );
-
-/**
- * Encodes a stream to Base64 and appends the encoded bytes to a string.
- *
- * @tparam ToStringType The string type.
- * @param from The istream to read from until EOF is reached.
- * @param to The string to append the encoded bytes to.
- * @return Returns the number of encoded bytes.
- */
-template<class ToStringType>
-typename std::enable_if<ZORBA_IS_STRING(ToStringType),size_type>::type
-encode( std::istream &from, ToStringType *to ) {
-  size_type total_encoded = 0;
-  while ( !from.eof() ) {
-    char from_buf[ 1024 * 3 ], to_buf[ 1024 * 4 ];
-    from.read( from_buf, sizeof from_buf );
-    if ( std::streamsize const gcount = from.gcount() ) {
-      size_type const encoded =
-        encode( from_buf, static_cast<size_type>( gcount ), to_buf );
-      to->append( to_buf, encoded );
-      total_encoded += encoded;
-    } else
-      break;
-  }
-  return total_encoded;
-}
-
-/**
- * Base64-encodes a stream and appends the encoded bytes onto a
- * vector&lt;char;&gt;.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The vector to append the encoded bytes to.
- * @param Returns the number of encoded bytes.
- */
-size_type encode( std::istream &from, std::vector<char> *to );
-
-///////////////////////////////////////////////////////////////////////////////
-
-} // namespace base64
-} // namespace zorba
-
-#endif /* ZORBA_BASE64_UTIL_H */
-/*
- * Local variables:
- * mode: c++
- * End:
- */
-/* vim:set et sw=2 ts=2: */

=== modified file 'src/util/stream_util.h'
--- src/util/stream_util.h	2013-06-05 05:11:00 +0000
+++ src/util/stream_util.h	2013-07-30 18:41:28 +0000
@@ -63,18 +63,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 /**
- * Reads from the given istream until \a n non-whitespace characters are read
- * or until EOF is encountered.
- *
- * @param is The istream to read from.
- * @param buf A pointer to the start of a buffer to read into.
- * @param n The number of non-whitespace characters to read.
- * @return Returns the number of non-whitespace characters read.
- */
-std::streamsize read_without_whitespace( std::istream &is, char *buf,
-                                         std::streamsize n );
-
-/**
  * Emits an integer as Roman numerals to the given ostream.  By default,
  * numerals are emitted in lower-case.  To emit in upper-case, set the
  * \c uppercase format flag on the stream.

=== modified file 'src/zorbatypes/binary.cpp'
--- src/zorbatypes/binary.cpp	2013-06-21 01:06:29 +0000
+++ src/zorbatypes/binary.cpp	2013-07-30 18:41:28 +0000
@@ -20,15 +20,14 @@
 #include <zorba/error.h>
 #include <string>
 
-#include "zorbatypes/binary.h"
+#include <zorba/util/base64_util.h>
 
 #include "diagnostics/xquery_diagnostics.h"
-
 #include "util/ascii_util.h"
-#include "util/base64_util.h"
 #include "util/hash/hash.h"
 #include "util/hexbinary_util.h"
 #include "util/stl_util.h"
+#include "zorbatypes/binary.h"
 
 using namespace std;
 


Follow ups