home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / wireformat.js < prev    next >
Text File  |  2006-08-07  |  4KB  |  118 lines

  1. function PROT_VersionParser(type, opt_major, opt_minor, opt_requireMac) {
  2. this.debugZone = "versionparser";
  3. this.type = type;
  4. this.major = 0;
  5. this.minor = 0;
  6. this.badHeader = false;
  7. this.mac = false;
  8. this.macval = "";
  9. this.macFailed = false;
  10. this.requireMac = !!opt_requireMac;
  11. this.update = false;
  12. this.needsUpdate = false;  // used by ListManager to determine update policy
  13. this.didRead = false;
  14. if (opt_major)
  15. this.major = parseInt(opt_major);
  16. if (opt_minor)
  17. this.minor = parseInt(opt_minor);
  18. }
  19. PROT_VersionParser.prototype.ImportVersion = function(version) {
  20. this.major = version.major;
  21. this.minor = version.minor;
  22. this.mac = version.mac;
  23. this.macFailed = version.macFailed;
  24. this.macval = version.macval;
  25. }
  26. PROT_VersionParser.prototype.toString = function() {
  27. var s = "[" + this.type + " " + this.major + "." + this.minor + "]";
  28. return s;
  29. }
  30. PROT_VersionParser.prototype.versionString = function() {
  31. return this.major + "." + this.minor;
  32. }
  33. PROT_VersionParser.prototype.toUrl = function() {
  34. return this.major + ":" + this.minor;
  35. }
  36. PROT_VersionParser.prototype.processOldFormat_ = function(line) {
  37. if (line[0] != '[' || line.slice(-1) != ']')
  38. return false;
  39. var description = line.slice(1, -1);
  40. var tokens = description.split(" ");
  41. this.type = tokens[0];
  42. var majorminor = tokens[1].split(".");
  43. this.major = parseInt(majorminor[0]);
  44. this.minor = parseInt(majorminor[1]);
  45. if (isNaN(this.major) || isNaN(this.minor))
  46. return false;
  47. if (tokens.length >= 3) {
  48. this.update = tokens[2] == "update";
  49. }
  50. return true;
  51. }
  52. PROT_VersionParser.prototype.fromString = function(line) {
  53. G_Debug(this, "Calling fromString with line: " + line);
  54. if (line[0] != '[' || line.slice(-1) != ']')
  55. return false;
  56. var secondBracket = line.indexOf('[', 1);
  57. var firstPart = null;
  58. var secondPart = null;
  59. if (secondBracket != -1) {
  60. firstPart = line.substring(0, secondBracket);
  61. secondPart = line.substring(secondBracket);
  62. G_Debug(this, "First part: " + firstPart + " Second part: " + secondPart);
  63. } else {
  64. firstPart = line;
  65. G_Debug(this, "Old format: " + firstPart);
  66. }
  67. if (!this.processOldFormat_(firstPart))
  68. return false;
  69. if (secondPart && !this.processOptTokens_(secondPart))
  70. return false;
  71. return true;
  72. }
  73. PROT_VersionParser.prototype.processOptTokens_ = function(line) {
  74. if (line[0] != '[' || line.slice(-1) != ']')
  75. return false;
  76. var description = line.slice(1, -1);
  77. var tokens = description.split(" ");
  78. for (var i = 0; i < tokens.length; i++) {
  79. G_Debug(this, "Processing optional token: " + tokens[i]);
  80. var tokenparts = tokens[i].split("=");
  81. switch(tokenparts[0]){
  82. case "mac":
  83. this.mac = true;
  84. if (tokenparts.length < 2) {
  85. G_Debug(this, "Found mac flag but not mac value!");
  86. return false;
  87. }
  88. this.macval = tokens[i].substr(tokens[i].indexOf("=")+1);
  89. break;
  90. default:
  91. G_Debug(this, "Found unrecognized token: " + tokenparts[0]);
  92. break;
  93. }
  94. }
  95. return true;
  96. }
  97. function TEST_PROT_WireFormat() {
  98. if (G_GDEBUG) {
  99. var z = "versionparser UNITTEST";
  100. G_Debug(z, "Starting");
  101. var vp = new PROT_VersionParser("dummy");
  102. G_Assert(z, vp.fromString("[foo-bar-url 1.234]"),
  103. "failed to parse old format");
  104. G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type");
  105. G_Assert(z, "1" == vp.major, "failed to parse major");
  106. G_Assert(z, "234" == vp.minor, "failed to parse minor");
  107. vp = new PROT_VersionParser("dummy");
  108. G_Assert(z, vp.fromString("[foo-bar-url 1.234][mac=567]"),
  109. "failed to parse new format");
  110. G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type");
  111. G_Assert(z, "1" == vp.major, "failed to parse major");
  112. G_Assert(z, "234" == vp.minor, "failed to parse minor");
  113. G_Assert(z, true == vp.mac, "failed to parse mac");
  114. G_Assert(z, "567" == vp.macval, "failed to parse macval");
  115. G_Debug(z, "PASSED");
  116. }
  117. }
  118.