Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sbcs-codec.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that
  2. // correspond to encoded bytes (if 128 - then lower half is ASCII).
  3. exports._sbcs = function(options) {
  4. if (!options)
  5. throw new Error("SBCS codec is called without the data.")
  6. // Prepare char buffer for decoding.
  7. if (!options.chars || (options.chars.length !== 128 && options.chars.length !== 256))
  8. throw new Error("Encoding '"+options.type+"' has incorrect 'chars' (must be of len 128 or 256)");
  9. if (options.chars.length === 128) {
  10. var asciiString = "";
  11. for (var i = 0; i < 128; i++)
  12. asciiString += String.fromCharCode(i);
  13. options.chars = asciiString + options.chars;
  14. }
  15. var decodeBuf = new Buffer(options.chars, 'ucs2');
  16. // Encoding buffer.
  17. var encodeBuf = new Buffer(65536);
  18. encodeBuf.fill(options.iconv.defaultCharSingleByte.charCodeAt(0));
  19. for (var i = 0; i < options.chars.length; i++)
  20. encodeBuf[options.chars.charCodeAt(i)] = i;
  21. return {
  22. encoder: encoderSBCS,
  23. decoder: decoderSBCS,
  24. encodeBuf: encodeBuf,
  25. decodeBuf: decodeBuf,
  26. };
  27. }
  28. function encoderSBCS(options) {
  29. return {
  30. write: encoderSBCSWrite,
  31. end: function() {},
  32. encodeBuf: this.encodeBuf,
  33. };
  34. }
  35. function encoderSBCSWrite(str) {
  36. var buf = new Buffer(str.length);
  37. for (var i = 0; i < str.length; i++)
  38. buf[i] = this.encodeBuf[str.charCodeAt(i)];
  39. return buf;
  40. }
  41. function decoderSBCS(options) {
  42. return {
  43. write: decoderSBCSWrite,
  44. end: function() {},
  45. decodeBuf: this.decodeBuf,
  46. };
  47. }
  48. function decoderSBCSWrite(buf) {
  49. // Strings are immutable in JS -> we use ucs2 buffer to speed up computations.
  50. var decodeBuf = this.decodeBuf;
  51. var newBuf = new Buffer(buf.length*2);
  52. var idx1 = 0, idx2 = 0;
  53. for (var i = 0, _len = buf.length; i < _len; i++) {
  54. idx1 = buf[i]*2; idx2 = i*2;
  55. newBuf[idx2] = decodeBuf[idx1];
  56. newBuf[idx2+1] = decodeBuf[idx1+1];
  57. }
  58. return newBuf.toString('ucs2');
  59. }