Permalink
Browse files

[enhance] stdlib: Add standar binary <-> string conversion (raw, hex,…

… base64)
  • Loading branch information...
1 parent e0073bc commit 4473302a0b5467d45d665130eb663f5e441f1f7b @BourgerieQuentin BourgerieQuentin committed Sep 26, 2012
View
31 lib/plugins/opabsl/mlbsl/bslBinary.ml
@@ -6,21 +6,29 @@
##register length\ `Buf.length`: binary -> int
-##register binary_of_string : string -> binary
-let binary_of_string s =
- let b = Buf.create (String.length s) in
- Buf.add_string b s;
- b
+exception BslBinaryError of string
+let error str = raise (BslBinaryError str)
-##register binary_of_string8 : string -> binary
-let binary_of_string8 s =
+let binary_of_string s =
let b = Buf.create (String.length s) in
Buf.add_string b s;
b
-##register string_of_binary\ `Buf.contents`: binary -> string
-
-##register string_of_binary8\ `Buf.contents`: binary -> string
+##register of_encoding : string, string -> binary
+let of_encoding s e =
+ match e with
+ | "utf8" -> binary_of_string s
+ | "hex" -> binary_of_string (BaseString.from_hex s)
+ | "base64" -> binary_of_string (BaseString.base64decode s)
+ | _ -> error (Printf.sprintf "BslBinary.of_encoding: unknown encoding %s" e)
+
+##register to_encoding : binary, string -> string
+let to_encoding b e =
+ match e with
+ | "utf8" -> Buf.contents b
+ | "hex" -> BaseString.to_hex (Buf.contents b)
+ | "base64" -> BaseString.base64encode (Buf.contents b)
+ | _ -> error (Printf.sprintf "BslBinary.to_encoding: unknown encoding %s" e)
##register resize : binary, int -> void
let resize b size =
@@ -64,9 +72,6 @@ let add_binaryr b nb =
Buf.add_string b (Buf.contents nb);
BslUtils.create_outcome (`success ())
-exception BslBinaryError of string
-let error str = raise (BslBinaryError str)
-
##register add_int8 : binary, int -> void
let add_int8 b i =
(* if (i < -0x80 || i > 0x7f) then error (Printf.sprintf "BslBinary.add_int8: out of range int %d" i); *)
View
34 lib/plugins/opabsl/nodejsbsl/bslBinary.nodejs
@@ -29,27 +29,6 @@ function binary_length(b) {
return b.length;
}
-/**
- * @register {string -> binary}
- */
-function binary_of_string(s) {
- return binary_of_buffer(new Buffer(s));
-}
-
-/**
- * @register {string -> binary}
- */
-function binary_of_string8(s){
- return binary_of_buffer(new Buffer(s,'binary'));
-}
-
-/**
- * @register {binary -> string}
- */
-function string_of_binary8(b){
- return string_of_binary8(b);
-}
-
/*
* We do actually need this, at least for the mongo driver.
* The routine below is for 7-bit characters *only* it actually
@@ -74,10 +53,17 @@ function string_of_binary8(b)
}
/**
- * @register {binary -> string}
+ * @register {string, string -> binary} of_encoding
+ */
+function binary_of_string(d, e){
+ return binary_of_buffer(new Buffer(d, e));
+}
+
+/**
+ * @register {binary, string -> string} to_encoding
*/
-function string_of_binary(b) {
- return b.contents.toString();
+function string_of_binary(d, e){
+ return d.contents.toString(e);
}
/**
View
8 lib/plugins/opabsl/nodejsbsl/bslPervasivesServer.nodejs
@@ -10,18 +10,10 @@
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-var Buffer = require('buffer').Buffer;
-
/**
* @register { -> void}
*/
function flush_all() {
return js_void;
}
-/**
- * @register { string -> binary}
- */
-function bin_of_base64(s) {
- return binary_of_buffer(new Buffer(s, 'base64'));
-}
View
2 lib/stdlib/apis/oauth/oauth.opa
@@ -107,7 +107,7 @@ type OAuth.token_res = { success : OAuth.token } / { error : string }
base_string = build_base_string(uri, params, auth_params)
do API_libs_private.apijlog("Base string: {base_string}")
key = "{p.consumer_secret}&{secret}"
- res = Crypto.Base64.encode(binary_of_string(Crypto.Hash.hmac_sha1(key, base_string)))
+ res = Crypto.Base64.encode(Crypto.HMAC.sha1(key, Binary.of_string(base_string)))
res = API_libs_private.url_encoder(res)
do API_libs_private.apijlog("Signature: {res}")
res
View
103 lib/stdlib/core/binary.opa
@@ -64,51 +64,78 @@ type binary = external
* {1 Interface}
*/
-/**
- * Create binary data from a string.
- *
- * Copies the entire string.
- *
- * Note: Beware of the behaviour of multi-byte characters which may not be portable
- * between the different backends.
- *
- * @param s The string to copy.
- * @return A binary type set exactly to the size of the string.
- */
-binary_of_string : string -> binary = %%BslBinary.binary_of_string%%
-
-/**
- * As for string_of_binary but makes an attempt to split multi-byte
- * characters into separate 8-bit characters. This routine should be
- * used with caution, it is slow and makes assumptions about the
- * behaviour of the backend.
- */
-binary_of_string8 : string -> binary = %%BslBinary.binary_of_string8%%
-
-/**
- * Turns binary data back into a string.
- *
- * As for binary_of_string this function copies the entire string.
- *
- * Multi-byte characters are not reconstituted.
- *
- * @param b The binary data.
- * @return A string of 8-bit characters.
- */
-string_of_binary : binary -> string = %%BslBinary.string_of_binary%%
+binary_of_string : string -> binary = Binary.of_string
-/**
- * As for string_of_binary but uses the *deprecated* binary encoding
- * for javascript backends. Use with caution.
- */
-string_of_binary8 : binary -> string = %%BslBinary.string_of_binary%%
+string_of_binary : binary -> string = Binary.to_string
@opacapi
-bin_of_base64(x:string):binary = %%bslPervasivesServer.bin_of_base64%%(x)
+bin_of_base64 = Binary.of_base64
Binary = {{
/**
+ * A generic to_string function.
+ * @param encoding is wanted encoding
+ * @param data is binary data to encode
+ * @returns An encoded string of [data]
+ */
+ @private
+ to_encoding(data, encoding) = %%bslBinary.to_encoding%%(data, encoding)
+
+ /**
+ * Returns the string content of a binary [data].
+ * Note: The binary data *must* be valid utf8 sequence else the resulted string
+ * can be invalid.
+ * @param data The binary data
+ * @return The decoded content of [data]
+ */
+ to_string(data) = to_encoding(data, "utf8")
+
+ /**
+ * Returns a base64 encoded string of a binary [data].
+ * @param data The binary data
+ * @return The base64 encoded content of [data]
+ */
+ to_base64(data) = to_encoding(data, "base64")
+
+ /**
+ * Returns a hex encoded string of a binary [data].
+ * @param data The binary data
+ * @return The hex encoded content of [data]
+ */
+ to_hex(data) = to_encoding(data, "hex")
+
+ /**
+ * A generic of_string function.
+ * @param encoding is the way where the string is encoded
+ * @param data is string data to decode to a binary
+ * @return The decoded binary of [data]
+ */
+ @private
+ of_encoding(data, encoding) = %%bslBinary.of_encoding%%(data, encoding)
+
+ /**
+ * Convert a (non-encoded) string to its binary representation.
+ * @param data The string to convert
+ * @return The binary representation of [data]
+ */
+ of_string(data) = of_encoding(data, "utf8")
+
+ /**
+ * Convert a base64 encoded string to its binary representation.
+ * @param data The string to convert
+ * @return The binary representation of base64 decoded [data]
+ */
+ of_base64(data) = of_encoding(data, "base64")
+
+ /**
+ * Convert a hex encoded string to its binary representation.
+ * @param data The string to convert
+ * @return The binary representation of hex decoded [data]
+ */
+ of_hex(data) = of_encoding(data, "hex")
+
+ /**
* Create binary data of the given size.
*
* @param hint The initial size.

0 comments on commit 4473302

Please sign in to comment.