binom.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. class BinOM {
  2. constructor(data) {
  3. }
  4. };
  5. (()=>{
  6. class MemoryBuffer extends ArrayBuffer{
  7. resize(new_byte_length) {
  8. ArrayBuffer.transfer(this, new_byte_length)
  9. }
  10. };
  11. function isInt(n){
  12. return Number(n) === n && n % 1 === 0;
  13. }
  14. function isFloat(n){
  15. return Number(n) === n && n % 1 !== 0;
  16. }
  17. BinOM.Byte = class Byte {
  18. constructor(value) {
  19. if(typeof value != "number")
  20. throw new Error("Invalid value type");
  21. let data = new Uint8Array(1);
  22. data[0] = value;
  23. Object.defineProperties(this, {
  24. data: {
  25. get: () => data[0],
  26. set: value => data[0] = value
  27. },
  28. serialized: {
  29. get: () => {
  30. let buffer = new DataView(new ArrayBuffer(2));
  31. buffer.setUint8(0, this.constructor.type_number);
  32. buffer.setUint8(1, this.data);
  33. return buffer.buffer;
  34. }
  35. }
  36. });
  37. }
  38. valueOf() {return this.data;}
  39. };
  40. BinOM.Word = class Word {
  41. constructor(value) {
  42. if(typeof value != "number")
  43. throw new Error("Invalid value type");
  44. let data = new Uint16Array(1);
  45. data[0] = value;
  46. Object.defineProperties(this, {
  47. data: {
  48. get: () => data[0],
  49. set: value => data[0] = value
  50. },
  51. serialized: {
  52. get: () => {
  53. let buffer = new DataView(new ArrayBuffer(3));
  54. buffer.setUint8(0, this.constructor.type_number);
  55. buffer.setUint16(1, this.data);
  56. return buffer.buffer;
  57. }
  58. }
  59. });
  60. }
  61. valueOf() {return this.data;}
  62. };
  63. BinOM.DWord = class DWord {
  64. constructor(value) {
  65. if(typeof value != "number")
  66. throw new Error("Invalid value type");
  67. let data = new Uint32Array(1);
  68. data[0] = value;
  69. Object.defineProperties(this, {
  70. data: {
  71. get: () => data[0],
  72. set: value => data[0] = value
  73. },
  74. serialized: {
  75. get: () => {
  76. let buffer = new DataView(new ArrayBuffer(5));
  77. buffer.setUint8(0, this.constructor.type_number);
  78. buffer.setUint32(1, this.data);
  79. return buffer.buffer;
  80. }
  81. }
  82. });
  83. }
  84. valueOf() {return this.data;}
  85. };
  86. BinOM.QWord = class QWord {
  87. constructor(value) {
  88. if(typeof value == "number")
  89. value = BigInt(value)
  90. else if(typeof value != "bigint")
  91. throw new Error("Invalid value type");
  92. let data = new BigUint64Array(1);
  93. data[0] = value;
  94. Object.defineProperties(this, {
  95. data: {
  96. get: () => data[0],
  97. set: value => data[0] = BigInt(value)
  98. },
  99. serialized: {
  100. get: () => {
  101. let buffer = new DataView(new ArrayBuffer(9));
  102. buffer.setUint8(0, this.constructor.type_number);
  103. buffer.setBigUint64(1, this.data);
  104. return buffer.buffer;
  105. }
  106. }
  107. });
  108. }
  109. valueOf() {return this.data;}
  110. };
  111. BinOM.ByteArray = class ByteArray {
  112. constructor(array_data) {
  113. let data;
  114. if(typeof array_data == "string")
  115. data = new TextEncoder("utf-8").encode(array_data).buffer;
  116. else if(array_data.constructor.name == "Array")
  117. data = new Uint8Array(array_data).buffer;
  118. else if(array_data.constructor.name == "Uint8Array" ||
  119. array_data.constructor.name == "Int8Array" ||
  120. array_data.constructor.name == "Uint16Array" ||
  121. array_data.constructor.name == "Int16Array" ||
  122. array_data.constructor.name == "Uint32Array" ||
  123. array_data.constructor.name == "Int32Array" ||
  124. array_data.constructor.name == "BigUint64Array" ||
  125. array_data.constructor.name == "BigInt64Array")
  126. data = array_data.buffer;
  127. Object.defineProperties(this, {
  128. data: {get: () => new Uint8Array(data), set: new_array_data => {
  129. if(typeof array_data == "string")
  130. data = new TextEncoder("utf-8").encode(array_data).buffer;
  131. else if(array_data.constructor.name == "Array")
  132. data = new Uint8Array(array_data).buffer;
  133. else if(array_data.constructor.name == "Uint8Array" ||
  134. array_data.constructor.name == "Int8Array" ||
  135. array_data.constructor.name == "Uint16Array" ||
  136. array_data.constructor.name == "Int16Array" ||
  137. array_data.constructor.name == "Uint32Array" ||
  138. array_data.constructor.name == "Int32Array" ||
  139. array_data.constructor.name == "BigUint64Array" ||
  140. array_data.constructor.name == "BigInt64Array")
  141. data = array_data.buffer;
  142. return new Uint8Array(data);
  143. }}
  144. })
  145. }
  146. valueOf() {return this.data}
  147. get(index) {return this.data[index];}
  148. set(index, value) {return this.data[index] = value;}
  149. };
  150. BinOM.WordArray = class WordArray {};
  151. BinOM.DWordArray = class DWordArray {};
  152. BinOM.QWordArray = class QWordArray {};
  153. BinOM.Array = class BinOMArray {};
  154. BinOM.Object = class BinOMObject {};
  155. Object.defineProperties(BinOM.Byte, {type_number:{get: () => 0x01}});
  156. Object.defineProperties(BinOM.Word, {type_number:{get: () => 0x02}});
  157. Object.defineProperties(BinOM.DWord, {type_number:{get: () => 0x03}});
  158. Object.defineProperties(BinOM.QWord, {type_number:{get: () => 0x04}});
  159. Object.defineProperties(BinOM.ByteArray, {type_number:{get: () => 0x05}});
  160. Object.defineProperties(BinOM.WordArray, {type_number:{get: () => 0x06}});
  161. Object.defineProperties(BinOM.DWordArray, {type_number:{get: () => 0x07}});
  162. Object.defineProperties(BinOM.QWordArray, {type_number:{get: () => 0x08}});
  163. Object.defineProperties(BinOM.Array, {type_number:{get: () => 0x09}});
  164. Object.defineProperties(BinOM.Object, {type_number:{get: () => 0x0A}});
  165. })()
  166. const ui8 = BinOM.Byte;
  167. const ui16 = BinOM.Word;
  168. const ui32 = BinOM.DWord;
  169. const ui64 = BinOM.QWord;