ClientAuthenticationPacket.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var Buffer = require('safe-buffer').Buffer;
  2. module.exports = ClientAuthenticationPacket;
  3. function ClientAuthenticationPacket(options) {
  4. options = options || {};
  5. this.clientFlags = options.clientFlags;
  6. this.maxPacketSize = options.maxPacketSize;
  7. this.charsetNumber = options.charsetNumber;
  8. this.filler = undefined;
  9. this.user = options.user;
  10. this.scrambleBuff = options.scrambleBuff;
  11. this.database = options.database;
  12. this.protocol41 = options.protocol41;
  13. }
  14. ClientAuthenticationPacket.prototype.parse = function(parser) {
  15. if (this.protocol41) {
  16. this.clientFlags = parser.parseUnsignedNumber(4);
  17. this.maxPacketSize = parser.parseUnsignedNumber(4);
  18. this.charsetNumber = parser.parseUnsignedNumber(1);
  19. this.filler = parser.parseFiller(23);
  20. this.user = parser.parseNullTerminatedString();
  21. this.scrambleBuff = parser.parseLengthCodedBuffer();
  22. this.database = parser.parseNullTerminatedString();
  23. } else {
  24. this.clientFlags = parser.parseUnsignedNumber(2);
  25. this.maxPacketSize = parser.parseUnsignedNumber(3);
  26. this.user = parser.parseNullTerminatedString();
  27. this.scrambleBuff = parser.parseBuffer(8);
  28. this.database = parser.parseLengthCodedBuffer();
  29. }
  30. };
  31. ClientAuthenticationPacket.prototype.write = function(writer) {
  32. if (this.protocol41) {
  33. writer.writeUnsignedNumber(4, this.clientFlags);
  34. writer.writeUnsignedNumber(4, this.maxPacketSize);
  35. writer.writeUnsignedNumber(1, this.charsetNumber);
  36. writer.writeFiller(23);
  37. writer.writeNullTerminatedString(this.user);
  38. writer.writeLengthCodedBuffer(this.scrambleBuff);
  39. writer.writeNullTerminatedString(this.database);
  40. } else {
  41. writer.writeUnsignedNumber(2, this.clientFlags);
  42. writer.writeUnsignedNumber(3, this.maxPacketSize);
  43. writer.writeNullTerminatedString(this.user);
  44. writer.writeBuffer(this.scrambleBuff);
  45. if (this.database && this.database.length) {
  46. writer.writeFiller(1);
  47. writer.writeBuffer(Buffer.from(this.database));
  48. }
  49. }
  50. };