bencode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2009 Anton Ekblad
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software. */
  10. self.addEventListener('message', function(e) {
  11. self.postMessage(bdecode(e.data));
  12. }, false);
  13. // bencode an object
  14. function bencode(obj) {
  15. switch(btypeof(obj)) {
  16. case "string": return bstring(obj);
  17. case "number": return bint(obj);
  18. case "list": return blist(obj);
  19. case "dictionary": return bdict(obj);
  20. default: return null;
  21. }
  22. }
  23. // decode a bencoded string into a javascript object
  24. function bdecode(str) {
  25. var dec = bparse(str);
  26. if(dec != null && dec[1] == "")
  27. return dec[0];
  28. return null;
  29. }
  30. // parse a bencoded string; bdecode is really just a wrapper for this one.
  31. // all bparse* functions return an array in the form
  32. // [parsed object, remaining string to parse]
  33. function bparse(str) {
  34. switch(str.charAt(0)) {
  35. case "d": return bparseDict(str.substr(1));
  36. case "l": return bparseList(str.substr(1));
  37. case "i": return bparseInt(str.substr(1));
  38. default: return bparseString(str);
  39. }
  40. }
  41. // parse a bencoded string
  42. function bparseString(str) {
  43. str2 = str.split(":", 1)[0];
  44. if(isNum(str2)) {
  45. len = parseInt(str2);
  46. return [str.substr(str2.length+1, len),
  47. str.substr(str2.length+1+len)];
  48. }
  49. return null;
  50. }
  51. // parse a bencoded integer
  52. function bparseInt(str) {
  53. var str2 = str.split("e", 1)[0];
  54. if(!isNum(str2)) {
  55. return null;
  56. }
  57. return [str2, str.substr(str2.length+1)];
  58. }
  59. // parse a bencoded list
  60. function bparseList(str) {
  61. var p, list = [];
  62. while(str.charAt(0) != "e" && str.length > 0) {
  63. p = bparse(str);
  64. if(null == p)
  65. return null;
  66. list.push(p[0]);
  67. str = p[1];
  68. }
  69. if(str.length <= 0)
  70. return null;
  71. return [list, str.substr(1)];
  72. }
  73. // parse a bencoded dictionary
  74. function bparseDict(str) {
  75. var key, val, dict = {};
  76. while(str.charAt(0) != "e" && str.length > 0) {
  77. key = bparseString(str);
  78. if(null == key)
  79. return;
  80. val = bparse(key[1]);
  81. if(null == val)
  82. return null;
  83. dict[key[0]] = val[0];
  84. str = val[1];
  85. }
  86. if(str.length <= 0)
  87. return null;
  88. return [dict, str.substr(1)];
  89. }
  90. // is the given string numeric?
  91. function isNum(str) {
  92. var i, c;
  93. str = str.toString();
  94. if(str.charAt(0) == '-')
  95. i = 1;
  96. else
  97. i = 0;
  98. for(; i < str.length; i++) {
  99. c = str.charCodeAt(i);
  100. if(c < 48 || c > 57) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. // returns the bencoding type of the given object
  107. function btypeof(obj) {
  108. var type = typeof obj;
  109. if(type == "object") {
  110. if(typeof obj.length == "undefined")
  111. return "dictionary";
  112. return "list";
  113. }
  114. return type;
  115. }
  116. // bencode a string
  117. function bstring(str) {
  118. return (str.length + ":" + str);
  119. }
  120. // bencode an integer
  121. function bint(num) {
  122. return "i" + num + "e";
  123. }
  124. // bencode a list
  125. function blist(list) {
  126. var str, enclist;
  127. enclist = [];
  128. for(key in list) {
  129. enclist.push(bencode(list[key]));
  130. }
  131. enclist.sort();
  132. str = "l";
  133. for(key in enclist) {
  134. str += enclist[key];
  135. }
  136. return str + "e";
  137. }
  138. // bencode a dictionary
  139. function bdict(dict) {
  140. var str, enclist;
  141. enclist = []
  142. for(key in dict) {
  143. enclist.push(bstring(key) + bencode(dict[key]));
  144. }
  145. enclist.sort();
  146. str = "d";
  147. for(key in enclist) {
  148. str += enclist[key];
  149. }
  150. return str + "e";
  151. }