Navigation Menu

Skip to content

Commit

Permalink
[enhance] stdlib: Add standar binary <-> string conversion (raw, hex,…
Browse files Browse the repository at this point in the history
… base64)
  • Loading branch information
BourgerieQuentin committed Sep 26, 2012
1 parent e0073bc commit 4473302
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 84 deletions.
31 changes: 18 additions & 13 deletions lib/plugins/opabsl/mlbsl/bslBinary.ml
Expand Up @@ -6,21 +6,29 @@


##register length\ `Buf.length`: binary -> int ##register length\ `Buf.length`: binary -> int


##register binary_of_string : string -> binary exception BslBinaryError of string
let binary_of_string s = let error str = raise (BslBinaryError str)
let b = Buf.create (String.length s) in
Buf.add_string b s;
b


##register binary_of_string8 : string -> binary let binary_of_string s =
let binary_of_string8 s =
let b = Buf.create (String.length s) in let b = Buf.create (String.length s) in
Buf.add_string b s; Buf.add_string b s;
b b


##register string_of_binary\ `Buf.contents`: binary -> string ##register of_encoding : string, string -> binary

let of_encoding s e =
##register string_of_binary8\ `Buf.contents`: binary -> string 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 ##register resize : binary, int -> void
let resize b size = let resize b size =
Expand Down Expand Up @@ -64,9 +72,6 @@ let add_binaryr b nb =
Buf.add_string b (Buf.contents nb); Buf.add_string b (Buf.contents nb);
BslUtils.create_outcome (`success ()) BslUtils.create_outcome (`success ())


exception BslBinaryError of string
let error str = raise (BslBinaryError str)

##register add_int8 : binary, int -> void ##register add_int8 : binary, int -> void
let add_int8 b i = let add_int8 b i =
(* if (i < -0x80 || i > 0x7f) then error (Printf.sprintf "BslBinary.add_int8: out of range int %d" i); *) (* if (i < -0x80 || i > 0x7f) then error (Printf.sprintf "BslBinary.add_int8: out of range int %d" i); *)
Expand Down
34 changes: 10 additions & 24 deletions lib/plugins/opabsl/nodejsbsl/bslBinary.nodejs
Expand Up @@ -29,27 +29,6 @@ function binary_length(b) {
return b.length; 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. * We do actually need this, at least for the mongo driver.
* The routine below is for 7-bit characters *only* it actually * The routine below is for 7-bit characters *only* it actually
Expand All @@ -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) { function string_of_binary(d, e){
return b.contents.toString(); return d.contents.toString(e);
} }


/** /**
Expand Down
8 changes: 0 additions & 8 deletions lib/plugins/opabsl/nodejsbsl/bslPervasivesServer.nodejs
Expand Up @@ -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. 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} * @register { -> void}
*/ */
function flush_all() { function flush_all() {
return js_void; return js_void;
} }


/**
* @register { string -> binary}
*/
function bin_of_base64(s) {
return binary_of_buffer(new Buffer(s, 'base64'));
}
2 changes: 1 addition & 1 deletion lib/stdlib/apis/oauth/oauth.opa
Expand Up @@ -107,7 +107,7 @@ type OAuth.token_res = { success : OAuth.token } / { error : string }
base_string = build_base_string(uri, params, auth_params) base_string = build_base_string(uri, params, auth_params)
do API_libs_private.apijlog("Base string: {base_string}") do API_libs_private.apijlog("Base string: {base_string}")
key = "{p.consumer_secret}&{secret}" 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) res = API_libs_private.url_encoder(res)
do API_libs_private.apijlog("Signature: {res}") do API_libs_private.apijlog("Signature: {res}")
res res
Expand Down
103 changes: 65 additions & 38 deletions lib/stdlib/core/binary.opa
Expand Up @@ -64,50 +64,77 @@ type binary = external
* {1 Interface} * {1 Interface}
*/ */


/** binary_of_string : string -> binary = Binary.of_string
* 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%%


/** string_of_binary : binary -> string = Binary.to_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%%


@opacapi @opacapi
bin_of_base64(x:string):binary = %%bslPervasivesServer.bin_of_base64%%(x) bin_of_base64 = Binary.of_base64


Binary = {{ 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. * Create binary data of the given size.
* *
Expand Down

0 comments on commit 4473302

Please sign in to comment.