aboutsummaryrefslogtreecommitdiff
path: root/javascript/olm_inbound_group_session.js
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-12-14 11:43:00 +0000
committerRichard van der Hoff <richard@matrix.org>2016-12-14 12:05:56 +0000
commit76610c0a3af57b600211ea38bc28bcccabc6a86c (patch)
treef437b11274ed4ed9551ee4c8d105c659ee0e34ef /javascript/olm_inbound_group_session.js
parentf6c05be8c5d35e725a8a2ed5ad661398ac9f8cd2 (diff)
Allocate memory for message blobs on the heap
Messages can be very large, so we don't really want to allocate them on the stack. Switch to using the heap for them, and try to clean up some of the string handling while we're at it.
Diffstat (limited to 'javascript/olm_inbound_group_session.js')
-rw-r--r--javascript/olm_inbound_group_session.js66
1 files changed, 41 insertions, 25 deletions
diff --git a/javascript/olm_inbound_group_session.js b/javascript/olm_inbound_group_session.js
index 1b7fcfe..5815320 100644
--- a/javascript/olm_inbound_group_session.js
+++ b/javascript/olm_inbound_group_session.js
@@ -64,33 +64,49 @@ InboundGroupSession.prototype['create'] = restore_stack(function(session_key) {
InboundGroupSession.prototype['decrypt'] = restore_stack(function(
message
) {
- var message_array = array_from_string(message);
- var message_buffer = stack(message_array);
- var max_plaintext_length = inbound_group_session_method(
- Module['_olm_group_decrypt_max_plaintext_length']
- )(this.ptr, message_buffer, message_array.length);
- // caculating the length destroys the input buffer.
- // So we copy the array to a new buffer
- var message_buffer = stack(message_array);
- var plaintext_buffer = stack(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
- var message_index = stack(4);
- var plaintext_length = inbound_group_session_method(Module["_olm_group_decrypt"])(
- this.ptr,
- message_buffer, message_array.length,
- plaintext_buffer, max_plaintext_length,
- message_index
- );
+ var message_buffer, plaintext_buffer;
- // Pointer_stringify requires a null-terminated argument (the optional
- // 'len' argument doesn't work for UTF-8 data).
- Module['setValue'](
- plaintext_buffer+plaintext_length,
- 0, "i8"
- );
+ try {
+ message_buffer = malloc(message.length);
+ Module['writeAsciiToMemory'](message, message_buffer, true);
+
+ var max_plaintext_length = inbound_group_session_method(
+ Module['_olm_group_decrypt_max_plaintext_length']
+ )(this.ptr, message_buffer, message.length);
+
+ // caculating the length destroys the input buffer, so we need to re-copy it.
+ Module['writeAsciiToMemory'](message, message_buffer, true);
+
+ plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
+ var message_index = stack(4);
- return {
- "plaintext": Pointer_stringify(plaintext_buffer),
- "message_index": Module['getValue'](message_index, "i32")
+ var plaintext_length = inbound_group_session_method(
+ Module["_olm_group_decrypt"]
+ )(
+ this.ptr,
+ message_buffer, message.length,
+ plaintext_buffer, max_plaintext_length,
+ message_index
+ );
+
+ // UTF8ToString requires a null-terminated argument, so add the
+ // null terminator.
+ Module['setValue'](
+ plaintext_buffer+plaintext_length,
+ 0, "i8"
+ );
+
+ return {
+ "plaintext": UTF8ToString(plaintext_buffer),
+ "message_index": Module['getValue'](message_index, "i32")
+ }
+ } finally {
+ if (message_buffer !== undefined) {
+ free(message_buffer);
+ }
+ if (plaintext_buffer !== undefined) {
+ free(plaintext_buffer);
+ }
}
});