file.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * ===========================================================================
  3. *
  4. * Wolf3D Browser Version GPL Source Code
  5. * Copyright (C) 2012 id Software LLC, a ZeniMax Media company.
  6. *
  7. * This file is part of the Wolf3D Browser Version GPL Source Code ("Wolf3D Browser Source Code").
  8. *
  9. * Wolf3D Browser Source Code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Wolf3D Browser Source Code is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License version 2
  20. * along with Wolf3D Browser Source Code. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * If you have questions concerning this license, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  23. *
  24. * ===========================================================================
  25. */
  26. /**
  27. * @namespace
  28. * @description Binary file reading
  29. */
  30. Wolf.File = (function() {
  31. /**
  32. * @description Open a file from URL
  33. * @memberOf Wolf.File
  34. * @param {string} url The URL to open
  35. * @param {function} callback Is called when file has been loaded. Second argument is file obj.
  36. */
  37. function openURL(url, callback) {
  38. var xhr = new XMLHttpRequest();
  39. xhr.onreadystatechange = function() {
  40. if (xhr.readyState == 4) {
  41. if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 0) {
  42. callback(null, {
  43. data : xhr.responseText,
  44. size : xhr.responseText.length,
  45. position : 0
  46. });
  47. } else {
  48. callback(new Error("Server returned HTTP status: " + xhr.status));
  49. }
  50. }
  51. }
  52. xhr.open("GET", url, true);
  53. xhr.overrideMimeType('text/plain; charset=x-user-defined');
  54. xhr.send(null);
  55. }
  56. function atob(str) {
  57. str = str.replace(/=+$/, "");
  58. var b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  59. a, b, c, b1, b2, b3, b4,
  60. chr = String.fromCharCode,
  61. out = [];
  62. for (var i=0,len=str.length;i<len;) {
  63. b1 = b64chars.indexOf(str.charAt(i++));
  64. b2 = b64chars.indexOf(str.charAt(i++));
  65. b3 = b64chars.indexOf(str.charAt(i++));
  66. b4 = b64chars.indexOf(str.charAt(i++));
  67. a = ((b1 & 0x3F) << 2) | ((b2 >> 4) & 0x3);
  68. b = ((b2 & 0xF) << 4) | ((b3 >> 2) & 0xF);
  69. c = ((b3 & 0x3) << 6) | (b4 & 0x3F);
  70. out.push(chr(a), chr(b), chr(c));
  71. }
  72. return out.join("");
  73. }
  74. /**
  75. * @description Open a file from base64 filetable
  76. * @memberOf Wolf.File
  77. * @param {string} filename The name of the file to open
  78. * @param {object} files The filetable
  79. * @param {function} callback Is called when file has been loaded. Second argument is file obj.
  80. */
  81. function open(filename, files, callback) {
  82. var b64data = files[filename];
  83. if (b64data) {
  84. var data = atob(b64data);
  85. callback(null, {
  86. data : data,
  87. size : data.length,
  88. position : 0
  89. });
  90. } else {
  91. callback(new Error("File not found: " + filename));
  92. }
  93. }
  94. /**
  95. * @description Read an unsigned 8-bit integer from a file and advance the file position.
  96. * @memberOf Wolf.File
  97. * @param {object} f The file
  98. * @returns {number}
  99. */
  100. function readUInt8(f) {
  101. var b = f.data.charCodeAt(f.position) & 0xFF
  102. f.position++;
  103. return b;
  104. }
  105. /**
  106. * @description Read a signed 8-bit integer from a file and advance the file position.
  107. * @memberOf Wolf.File
  108. * @param {object} f The file
  109. * @returns {number}
  110. */
  111. function readInt8(f) {
  112. var v = readUInt8(f);
  113. return v > 127 ? v - 256 : v;
  114. }
  115. /**
  116. * @description Read an unsigned 16-bit integer from a file and advance the file position.
  117. * @memberOf Wolf.File
  118. * @param {object} f The file
  119. * @returns {number}
  120. */
  121. function readUInt16(f) {
  122. var v = readUInt8(f) + (readUInt8(f) << 8);
  123. return (v < 0) ? v + 0x10000 : v;
  124. }
  125. /**
  126. * @description Read a signed 16-bit integer from a file and advance the file position.
  127. * @memberOf Wolf.File
  128. * @param {object} f The file
  129. * @returns {number}
  130. */
  131. function readInt16(f) {
  132. var v = readUInt16(f);
  133. return (v > 0x7fff) ? v - 0x10000 : v;
  134. }
  135. /**
  136. * @description Read an unsigned 32-bit integer from a file and advance the file position.
  137. * @memberOf Wolf.File
  138. * @param {object} f The file
  139. * @returns {number}
  140. */
  141. function readUInt32(f) {
  142. var b0 = readUInt8(f),
  143. b1 = readUInt8(f),
  144. b2 = readUInt8(f),
  145. b3 = readUInt8(f),
  146. v = ((((b3 << 8) + b2) << 8) + b1 << 8) + b0;
  147. return (v < 0) ? v + 0x100000000 : v;
  148. }
  149. /**
  150. * @description Read a signed 32-bit int from a file and advance the file position.
  151. * @memberOf Wolf.File
  152. * @param {object} f The file
  153. * @returns {number}
  154. */
  155. function readInt32(f) {
  156. var v = readUInt32(f);
  157. return (v > 0x7fffffff) ? v - 0x100000000 : v;
  158. }
  159. /**
  160. * @description Read a string from a file and advance the file position.
  161. * @memberOf Wolf.File
  162. * @param {object} f The file
  163. * @param {number} length The length of the string
  164. * @returns {string}
  165. */
  166. function readString(f, length) {
  167. var str = f.data.substr(f.position, length);
  168. f.position += length;
  169. return str;
  170. }
  171. /**
  172. * @description Read an array of bytes a file and advance the file position.
  173. * @memberOf Wolf.File
  174. * @param {object} f The file
  175. * @param {number} num The number of bytes to read
  176. * @returns {array}
  177. */
  178. function readBytes(f, num) {
  179. var b = [];
  180. for (var i=0;i<num;i++) {
  181. b[i] = f.data.charCodeAt(f.position+i) & 0xFF;
  182. }
  183. f.position += num;
  184. return b;
  185. }
  186. return {
  187. open : open,
  188. readInt8 : readInt8,
  189. readUInt8 : readUInt8,
  190. readInt16 : readInt16,
  191. readUInt16 : readUInt16,
  192. readInt32 : readInt32,
  193. readUInt32 : readUInt32,
  194. readBytes : readBytes,
  195. readString : readString
  196. };
  197. })();