Module: OverSIP::WebSocket::FramingUtils

Defined in:
ext/websocket_framing_utils/ws_framing_utils_ruby.c

Defined Under Namespace

Classes: Utf8Validator

Class Method Summary collapse

Class Method Details

.unmask(payload, mask) ⇒ Object

Ruby functions.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'ext/websocket_framing_utils/ws_framing_utils_ruby.c', line 17

VALUE WsFramingUtils_unmask(VALUE self, VALUE payload, VALUE mask)
{
  char *payload_str, *mask_str;
  long payload_len;  /* mask length is always 4 bytes. */
  char *unmasked_payload_str;
  VALUE rb_unmasked_payload;
  int i;

  if (TYPE(payload) != T_STRING)
    rb_raise(rb_eTypeError, "Argument must be a String");

  if (TYPE(mask) != T_STRING)
    rb_raise(rb_eTypeError, "Argument must be a String");

  if (RSTRING_LEN(mask) != 4)
    rb_raise(rb_eTypeError, "mask size must be 4 bytes");

  payload_str = RSTRING_PTR(payload);
  payload_len = RSTRING_LEN(payload);
  mask_str = RSTRING_PTR(mask);

  /* NOTE: In Ruby C extensions always use:
   *    pointer = ALLOC_N(type, n)
   * which means: pointer = (type*)xmalloc(sizeof(type)*(n))
   * and:
   *    xfree()
   */

  unmasked_payload_str = ALLOC_N(char, payload_len);

  for(i=0; i < payload_len; i++)
    unmasked_payload_str[i] = payload_str[i] ^ mask_str[i % 4];

  rb_unmasked_payload = rb_str_new(unmasked_payload_str, payload_len);
  xfree(unmasked_payload_str);
  return(rb_unmasked_payload);
}