ClientAuthenticationPacket.js 2.0 KB

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