zorba-coders team mailing list archive
-
zorba-coders team
-
Mailing list archive
-
Message #24731
[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:
Might as well change the public HexBinary API to match the new Base64 public API.
Requested reviews:
Paul J. Lucas (paul-lucas)
For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/178319
Might as well change the public HexBinary API to match the new Base64 public API.
--
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/178319
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog 2013-08-01 00:59:53 +0000
+++ ChangeLog 2013-08-02 15:05:39 +0000
@@ -38,6 +38,7 @@
* Fixed bug #1188280 (casting xs:id to xs:ncname)
* Fixed bug in casting to xs:NCName
* Replaced Base64 C++ API with a better one.
+ * Replaced HexBinary C++ API with a better one.
* Check that the namespace URI used in default namespace declaration is valid
* Fixed bug in error conditions for computed PI constructor
* Fixed implementation of fn:deep-equal according to latest W3C spec.
=== modified file 'include/zorba/util/base64_util.h'
--- include/zorba/util/base64_util.h 2013-07-30 18:35:31 +0000
+++ include/zorba/util/base64_util.h 2013-08-02 15:05:39 +0000
@@ -337,7 +337,7 @@
/**
* 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 A pointer to the 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.
@@ -349,7 +349,7 @@
* 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 A pointer to the 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.
=== modified file 'include/zorba/util/fs_util.h'
--- include/zorba/util/fs_util.h 2013-06-17 19:24:39 +0000
+++ include/zorba/util/fs_util.h 2013-08-02 15:05:39 +0000
@@ -397,7 +397,9 @@
template<class PathStringType>
iterator( PathStringType const &path,
typename std::enable_if<ZORBA_HAS_C_STR(PathStringType)
- >::type* = 0 ) : dir_path_( path.c_str() ) {
+ >::type* = nullptr ) :
+ dir_path_( path.c_str() )
+ {
ctor_impl();
}
=== modified file 'include/zorba/util/hexbinary_util.h'
--- include/zorba/util/hexbinary_util.h 2013-06-21 06:20:46 +0000
+++ include/zorba/util/hexbinary_util.h 2013-08-02 15:05:39 +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,347 @@
* limitations under the License.
*/
+#pragma once
#ifndef ZORBA_HEXBINARY_API_H
#define ZORBA_HEXBINARY_API_H
// standard
#include <iostream>
+#include <stdexcept>
+#include <sys/types.h> /* for size_t */
+#include <vector>
-// zorba
-#include <zorba/config.h>
-#include <zorba/zorba_string.h>
+// Zorba
+#include <zorba/internal/cxx_util.h>
+#include <zorba/internal/type_traits.h>
+#include <zorba/internal/ztd.h>
+#include <zorba/util/stream_util.h>
namespace zorba {
namespace hexbinary {
-///////////////////////////////////////////////////////////////////////////////
-
-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_ignore_ws = 0x01, ///< Ignore all whitespace.
+};
+
+////////// Exception //////////////////////////////////////////////////////////
+
+/**
+ * A %hexbinary::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 hexBinary-encoded
+ * bytes.
+ *
+ * @param n The number of bytes to decode.
+ * @return Returns the number of bytes needed for hexBinary decoding.
+ */
+inline size_type decoded_size( size_type n ) {
+ return n / 2;
+}
+
+/**
+ * Decodes a hexBinary-encoded buffer.
+ *
+ * @param from A pointer to the hexBinary 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 from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 hexBinary-encoded buffer and appends the decoded bytes onto a
+ * vector<char>.
+ *
+ * @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 from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 hexBinary-encoded buffer and writes the decoded bytes to the given
+ * stream.
+ *
+ * @param from A pointer to the hexBinary 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 from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 hexBinary-encoded buffer and appends the decoded bytes onto a
+ * string.
+ *
+ * @tparam ToStringType The string type.
+ * @param from A pointer to the hexBinary 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 decoding options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 hexBinary-encoded istream.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The ostream to write the decoded bytes to.
+ * @param options The decoding options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if an invalid byte is encountered.
+ */
+size_type decode( std::istream &from, std::ostream &to,
+ int options = dopt_none );
+
+/**
+ * Decodes a hexBinary-encoded istream and appends the decoded 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 decoded bytes to.
+ * @param options The decoding options to use.
+ * @return Returns the number of decoded bytes.
+ * @throws invalid_argument if \a from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 * 2 ], to_buf[ 1024 ];
+ 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 hexBinary-encoded stream and appends the decoded bytes onto a
+ * vector<char;>.
+ *
+ * @param from The istream to read from until EOF is reached.
+ * @param to The string to append the decoded bytes to.
+ * @param options The decoding options to use.
+ * @param Returns the number of decoded bytes.
+ * @throws invalid_argument if \a from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if an invalid byte is encountered.
+ */
+size_type decode( std::istream &from, std::vector<char> *to,
+ int options = dopt_none );
+
+/**
+ * Validates a hexBinary-encoded buffer.
+ *
+ * @param buf A pointer to the hexBinary buffer to be validated.
+ * @param buf_len The number of bytes to validate.
+ * @param options The decoding options to use.
+ * @throws invalid_argument if \a from_len (minus the amount of whitespace if
+ * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
+ * @throws hexbinary::exception if 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 hexBinary-encode \a n bytes.
+ *
+ * @param n The number of bytes to encode.
+ * @return Returns the number of bytes needed for hexBinary encoding.
+ */
+inline size_type encoded_size( size_type n ) {
+ return n * 2;
+}
+
+/**
+ * HexBinary-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 );
+
+/**
+ * HexBinary-encodes a buffer and appends the encoded bytes onto a
+ * vector<char>.
+ *
+ * @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 );
+
+/**
+ * HexBinary-encodes a buffer and writes the encoded bytes to the given stream.
+ *
+ * @param from A pointer to the 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 );
+
+/**
+ * HexBinary-encodes a buffer and appends the encoded bytes onto a string.
+ *
+ * @tparam ToStringType The string type.
+ * @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 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 ) );
+ }
+ return encoded;
+}
+
+/**
+ * HexBinary-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 hexBinary 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 * 2 ], to_buf[ 1024 ];
+ 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;
+}
+
+/**
+ * HexBinary-encodes a stream and appends the encoded bytes onto a
+ * vector<char;>.
+ *
+ * @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 );
///////////////////////////////////////////////////////////////////////////////
@@ -47,5 +362,9 @@
} // namespace zorba
#endif /* ZORBA_HEXBINARY_API_H */
+/*
+ * Local variables:
+ * mode: c++
+ * End:
+ */
/* vim:set et sw=2 ts=2: */
-
=== 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-30 18:35:31 +0000
+++ modules/http-client/json/http-client.xq.src/http_request_handler.cpp 2013-08-02 15:05:39 +0000
@@ -354,9 +354,7 @@
const char * lData = aItem.getHexBinaryValue(lLen);
if (aItem.isEncoded())
{
- String lEncoded(lData,lLen);
- String lDecodedData = zorba::hexbinary::decode(lEncoded);
- *theSerStream << lDecodedData;
+ zorba::hexbinary::decode(lData, lLen, *theSerStream);
}
else
{
=== modified file 'src/api/CMakeLists.txt'
--- src/api/CMakeLists.txt 2013-08-02 02:40:23 +0000
+++ src/api/CMakeLists.txt 2013-08-02 15:05:39 +0000
@@ -25,7 +25,7 @@
staticcontextimpl.cpp
dynamiccontextimpl.cpp
zorba_string.cpp
- hexbinaryimpl.cpp
+ hexbinary_util.cpp
hexbinary_streambuf.cpp
itemfactoryimpl.cpp
item.cpp
=== modified file 'src/api/hexbinary_streambuf.cpp'
--- src/api/hexbinary_streambuf.cpp 2013-06-20 00:53:29 +0000
+++ src/api/hexbinary_streambuf.cpp 2013-08-02 15:05:39 +0000
@@ -16,16 +16,16 @@
#include "stdafx.h"
+// standard
#include <stdexcept>
-
//#define ZORBA_DEBUG_HEXBINARY_STREAMBUF
#ifdef ZORBA_DEBUG_HEXBINARY_STREAMBUF
# include <stdio.h>
#endif
+// Zorba
#include <zorba/util/hexbinary_stream.h>
-
-#include "util/hexbinary_util.h"
+#include <zorba/util/hexbinary_util.h>
using namespace std;
=== renamed file 'src/util/hexbinary_util.cpp' => 'src/api/hexbinary_util.cpp'
--- src/util/hexbinary_util.cpp 2013-05-08 01:05:04 +0000
+++ src/api/hexbinary_util.cpp 2013-08-02 15:05:39 +0000
@@ -20,9 +20,10 @@
#include <algorithm>
#include <cstring>
-// local
-#include "hexbinary_util.h"
-#include "string_util.h"
+// Zorba
+#include <zorba/util/hexbinary_util.h>
+#include "util/mem_streambuf.h"
+#include "util/string_util.h"
using namespace std;
@@ -120,6 +121,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;
@@ -197,6 +206,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/hexbinaryimpl.cpp'
--- src/api/hexbinaryimpl.cpp 2013-06-21 06:20:46 +0000
+++ src/api/hexbinaryimpl.cpp 1970-01-01 00:00:00 +0000
@@ -1,83 +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/hexbinary_util.h>
-
-#include <zorba/config.h>
-#include <zorba/zorba_string.h>
-
-#include "diagnostics/dict.h"
-#include "diagnostics/xquery_exception.h"
-#include "util/hexbinary_util.h"
-#define CATCH_HEXBINARY_EXCEPTION() \
- catch (hexbinary::exception const &e) \
- { \
- throw XQUERY_EXCEPTION(err::FORG0001, \
- ERROR_PARAMS(ZED(FORG0001_BadHexDigit_2), e.invalid_char())); \
- } \
- catch (std::invalid_argument const&) \
- { \
- throw XQUERY_EXCEPTION(err::FORG0001, \
- ERROR_PARAMS(ZED(FORG0001_HexBinaryMustBeEven))); \
- }
-
-using namespace std;
-
-namespace zorba {
-namespace hexbinary {
-
-///////////////////////////////////////////////////////////////////////////////
-
-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_HEXBINARY_EXCEPTION()
-}
-
-
-String decode( istream &aStream ) {
- try {
- String result;
- decode( aStream, &result, dopt_ignore_ws );
- return result;
- }
- CATCH_HEXBINARY_EXCEPTION()
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-} // namespace hexbinary
-} // namespace zorba
-/* vim:set et sw=2 ts=2: */
=== modified file 'src/unit_tests/test_hexbinary.cpp'
--- src/unit_tests/test_hexbinary.cpp 2013-03-17 04:47:01 +0000
+++ src/unit_tests/test_hexbinary.cpp 2013-08-02 15:05:39 +0000
@@ -21,7 +21,7 @@
#include <stdexcept>
#include <string>
-#include "util/hexbinary_util.h"
+#include <zorba/util/hexbinary_util.h>
using namespace std;
using namespace zorba;
@@ -73,6 +73,19 @@
ASSERT_TRUE( no, out == expected );
}
+static void test_decode_buf_to_stream( int no, string const &in,
+ string const &expected ) {
+ hexbinary::size_type n = 0;
+ ostringstream oss;
+ ASSERT_NO_EXCEPTION(
+ no, n = hexbinary::decode( in.data(), in.size(), oss )
+ );
+ ASSERT_TRUE( no, n == expected.size() );
+ string const out( oss.str() );
+ ASSERT_TRUE( no, out.size() == expected.size() );
+ ASSERT_TRUE( no, out == expected );
+}
+
static void test_decode_buf_to_string( int no, string const &in,
string const &expected ) {
hexbinary::size_type n = 0;
@@ -148,6 +161,16 @@
ASSERT_TRUE( no, out == expected );
}
+static void test_encode_buf_to_stream( int no, string const &in,
+ string const &expected ) {
+ ostringstream oss;
+ hexbinary::size_type const n = hexbinary::encode( in.data(), in.size(), oss );
+ ASSERT_TRUE( no, n == expected.size() );
+ string const out( oss.str() );
+ ASSERT_TRUE( no, out.size() == expected.size() );
+ ASSERT_TRUE( no, out == expected );
+}
+
static void test_encode_buf_to_string( int no, string const &in,
string const &expected ) {
string out;
@@ -232,6 +255,7 @@
for ( test const *t = encode_tests; t->input; ++t, ++test_no ) {
test_encode_buf_to_buf( test_no, t->input, t->expected );
+ test_encode_buf_to_stream( test_no, t->input, t->expected );
test_encode_buf_to_string( test_no, t->input, t->expected );
test_encode_buf_to_vector( test_no, t->input, t->expected );
test_encode_stream_to_stream( test_no, t->input, t->expected );
@@ -241,6 +265,7 @@
for ( test const *t = decode_tests; t->input; ++t, ++test_no ) {
test_decode_buf_to_buf( test_no, t->input, t->expected );
+ test_decode_buf_to_stream( test_no, t->input, t->expected );
test_decode_buf_to_string( test_no, t->input, t->expected );
test_decode_buf_to_vector( test_no, t->input, t->expected );
test_decode_stream_to_stream( test_no, t->input, t->expected );
=== modified file 'src/util/CMakeLists.txt'
--- src/util/CMakeLists.txt 2013-07-30 18:35:31 +0000
+++ src/util/CMakeLists.txt 2013-08-02 15:05:39 +0000
@@ -17,7 +17,6 @@
dynamic_bitset.cpp
error_util.cpp
fs_util.cpp
- hexbinary_util.cpp
indent.cpp
json_parser.cpp
json_util.cpp
=== removed file 'src/util/hexbinary_util.h'
--- src/util/hexbinary_util.h 2013-06-01 00:30:39 +0000
+++ src/util/hexbinary_util.h 1970-01-01 00:00:00 +0000
@@ -1,341 +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_HEXBINARY_UTIL_H
-#define ZORBA_HEXBINARY_UTIL_H
-
-#include <iostream>
-#include <stdexcept>
-#include <sys/types.h> /* for size_t */
-#include <vector>
-
-#include <zorba/internal/cxx_util.h>
-
-#include "stream_util.h"
-
-namespace zorba {
-namespace hexbinary {
-
-////////// Types //////////////////////////////////////////////////////////////
-
-typedef size_t size_type;
-
-/**
- * Options to use for decoding.
- */
-enum decode_options {
- dopt_none = 0x00, ///< No options.
- dopt_ignore_ws = 0x01, ///< Ignore all whitespace.
-};
-
-////////// Exception //////////////////////////////////////////////////////////
-
-/**
- * A %hexbinary::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 hexBinary-encoded
- * bytes.
- *
- * @param n The number of bytes to decode.
- * @return Returns the number of bytes needed for hexBinary decoding.
- */
-inline size_type decoded_size( size_type n ) {
- return n / 2;
-}
-
-/**
- * Decodes a hexBinary-encoded buffer.
- *
- * @param from A pointer to the hexBinary 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 from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if 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 hexBinary-encoded buffer and appends the decoded bytes onto a
- * vector<char>.
- *
- * @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 from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if 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 hexBinary-encoded buffer and appends the decoded bytes onto a
- * string.
- *
- * @tparam ToStringType The string type.
- * @param from A pointer to the hexBinary 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 decoding options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if 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 hexBinary-encoded istream.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The ostream to write the decoded bytes to.
- * @param options The decoding options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if an invalid byte is encountered.
- */
-size_type decode( std::istream &from, std::ostream &to,
- int options = dopt_none );
-
-/**
- * Decodes a hexBinary-encoded istream and appends the decoded 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 decoded bytes to.
- * @param options The decoding options to use.
- * @return Returns the number of decoded bytes.
- * @throws invalid_argument if \a from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if 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 * 2 ], to_buf[ 1024 ];
- 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 hexBinary-encoded stream and appends the decoded bytes onto a
- * vector<char;>.
- *
- * @param from The istream to read from until EOF is reached.
- * @param to The string to append the decoded bytes to.
- * @param options The decoding options to use.
- * @param Returns the number of decoded bytes.
- * @throws invalid_argument if \a from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if an invalid byte is encountered.
- */
-size_type decode( std::istream &from, std::vector<char> *to,
- int options = dopt_none );
-
-/**
- * Validates a hexBinary-encoded buffer.
- *
- * @param buf A pointer to the hexBinary buffer to be validated.
- * @param buf_len The number of bytes to validate.
- * @param options The decoding options to use.
- * @throws invalid_argument if \a from_len (minus the amount of whitespace if
- * \a options contains \c dopt_ignore_ws) is not a multiple of 2.
- * @throws hexbinary::exception if 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 hexBinary-encode \a n bytes.
- *
- * @param n The number of bytes to encode.
- * @return Returns the number of bytes needed for hexBinary encoding.
- */
-inline size_type encoded_size( size_type n ) {
- return n * 2;
-}
-
-/**
- * HexBinary-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 );
-
-/**
- * HexBinary-encodes a buffer and appends the encoded bytes onto a
- * vector<char>.
- *
- * @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 );
-
-/**
- * HexBinary-encodes a buffer and appends the encoded bytes onto a string.
- *
- * @tparam ToStringType The string type.
- * @param from A pointer to the hexBinary 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 ) );
- }
- return encoded;
-}
-
-/**
- * HexBinary-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 hexBinary 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 * 2 ], to_buf[ 1024 ];
- 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;
-}
-
-/**
- * HexBinary-encodes a stream and appends the encoded bytes onto a
- * vector<char;>.
- *
- * @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 hexbinary
-} // namespace zorba
-
-#endif /* ZORBA_HEXBINARY_UTIL_H */
-/*
- * Local variables:
- * mode: c++
- * End:
- */
-/* vim:set et sw=2 ts=2: */
=== modified file 'src/zorbatypes/binary.cpp'
--- src/zorbatypes/binary.cpp 2013-07-30 18:35:31 +0000
+++ src/zorbatypes/binary.cpp 2013-08-02 15:05:39 +0000
@@ -21,11 +21,11 @@
#include <string>
#include <zorba/util/base64_util.h>
+#include <zorba/util/hexbinary_util.h>
#include "diagnostics/xquery_diagnostics.h"
#include "util/ascii_util.h"
#include "util/hash/hash.h"
-#include "util/hexbinary_util.h"
#include "util/stl_util.h"
#include "zorbatypes/binary.h"
Follow ups