Sha256: ae0a32846d85e2f930943923211066c3637ad4da3a392b4c6613f07f09da2577

Contents?: true

Size: 945 Bytes

Versions: 4

Compression:

Stored size: 945 Bytes

Contents

#include <ruby.h>

VALUE WebSocket = Qnil;
VALUE WebSocketMask = Qnil;

void Init_websocket_mask();
VALUE method_websocket_mask(VALUE self, VALUE payload, VALUE mask);

void Init_websocket_mask() {
  WebSocket = rb_define_module("WebSocket");
  WebSocketMask = rb_define_module_under(WebSocket, "Mask");
  rb_define_singleton_method(WebSocketMask, "mask", method_websocket_mask, 2);
}

VALUE method_websocket_mask(VALUE self, VALUE payload, VALUE mask) {
  if (mask == Qnil || RARRAY_LEN(mask) == 0) {
    return payload;
  }

  int n = RARRAY_LEN(payload), i, p, m;
  VALUE unmasked = rb_ary_new2(n);

  int mask_array[] = {
    NUM2INT(rb_ary_entry(mask, 0)),
    NUM2INT(rb_ary_entry(mask, 1)),
    NUM2INT(rb_ary_entry(mask, 2)),
    NUM2INT(rb_ary_entry(mask, 3))
  };

  for (i = 0; i < n; i++) {
    p = NUM2INT(rb_ary_entry(payload, i));
    m = mask_array[i % 4];
    rb_ary_store(unmasked, i, INT2NUM(p ^ m));
  }
  return unmasked;
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
websocket-driver-0.3.5 ext/websocket-driver/websocket_mask.c
websocket-driver-0.3.5-java ext/websocket-driver/websocket_mask.c
websocket-driver-0.3.4-java ext/websocket-driver/websocket_mask.c
websocket-driver-0.3.4 ext/websocket-driver/websocket_mask.c