displayer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. function HashTable() {
  2. let size = 0;
  3. let entry = new Object();
  4. this.add = function (key, value) {
  5. if (!this.containsKey(key)) {
  6. size++;
  7. }
  8. entry[key] = value;
  9. };
  10. this.getValue = function (key) {
  11. return this.containsKey(key) ? entry[key] : null;
  12. };
  13. this.remove = function (key) {
  14. if (this.containsKey(key) && (delete entry[key])) {
  15. size--;
  16. }
  17. };
  18. this.containsKey = function (key) {
  19. return (key in entry);
  20. };
  21. this.containsValue = function (value) {
  22. for (var prop in entry) {
  23. if (entry[prop] == value) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. };
  29. this.getValues = function () {
  30. var values = new Array();
  31. for (var prop in entry) {
  32. values.push(entry[prop]);
  33. }
  34. return values;
  35. };
  36. this.getKeys = function () {
  37. var keys = new Array();
  38. for (var prop in entry) {
  39. keys.push(prop);
  40. }
  41. return keys;
  42. };
  43. this.getSize = function () {
  44. return size;
  45. };
  46. this.clear = function () {
  47. size = 0;
  48. entry = new Object();
  49. }
  50. }
  51. let myuser;
  52. let lines;
  53. let routineTab = new HashTable();
  54. let vulTypeCount = new HashTable();
  55. let fileCount = new HashTable();
  56. $(document).ready(function () {
  57. let outhtml;
  58. try {
  59. outhtml = $(window.frames["opencc_vul"].document).html();
  60. }catch (e) {
  61. outhtml = $(window.frames["opencc_vul"].document)[0].body.innerHTML
  62. }
  63. if(outhtml === "" || outhtml == undefined || outhtml == null || outhtml.length == 0){
  64. $.ajax({url: "opencc_vul.txt"}).done(display_content);
  65. } else {
  66. display_content(outhtml);
  67. }
  68. });
  69. function display_content(s){
  70. let answer = s;
  71. let i = 0;
  72. let count = 0;
  73. let one_obj = {};
  74. let obj_list = [];
  75. let all_lines = "";
  76. lines = answer.split("\n");
  77. for(i = 0; i < lines.length ; i++){
  78. let n = lines[i].match(/[a-zA-Z\._:0-9]+/g);
  79. if(n != null) {
  80. let res = {}; // One line of result
  81. res.filename = n[0];
  82. res.vultype = n[4];
  83. res.variate = n[8];
  84. res.subroutine = n[9];
  85. res.lineno = n[1];
  86. if(!routineTab.containsKey
  87. (res.subroutine+"+"+res.variate+"+"+res.filename))
  88. {
  89. // Repetition Check Passes
  90. routineTab.add(res.subroutine+"+"+res.variate+"+"+res.filename, res.vultype);
  91. obj_list.push(res);
  92. all_lines += `L ${res.lineno} - <span> ${res.filename}</span> <br>`;
  93. count ++;
  94. if(vulTypeCount.containsKey(res.vultype)){
  95. let vul_list = vulTypeCount.getValue(res.vultype);
  96. vul_list.push(res);
  97. }else{
  98. let vul_list = new Array();
  99. vul_list.push(res);
  100. vulTypeCount.add(res.vultype, vul_list);
  101. }
  102. if(fileCount.containsKey(res.filename)){
  103. let vul_list = fileCount.getValue(res.filename);
  104. vul_list.push(res);
  105. }else{
  106. let vul_list = new Array();
  107. vul_list.push(res);
  108. fileCount.add(res.filename, vul_list);
  109. }
  110. }
  111. }
  112. }
  113. showToPage();
  114. }
  115. function showToPage() {
  116. let s = "";
  117. let keys = fileCount.getKeys();
  118. let one = [];
  119. let doms = "";
  120. for (i = 0; i<fileCount.getSize(); i++){
  121. one = fileCount.getValue(keys[i]);
  122. doms += makeOneFile(keys[i], one.length);
  123. }
  124. $("#perfile").html(doms);
  125. let bug_count = routineTab.getSize();
  126. $("#risk-counter").text(bug_count);
  127. $("#risky-f-counter").text(fileCount.getSize());
  128. $("#f-counter").text(131);
  129. $("#time-counter").text(21);
  130. showFiles();
  131. showVul();
  132. }
  133. function makeOneFile(file_name, file_count) {
  134. return `<li class="list-group-item btn btn-outline-primary d-flex justify-content-between align-items-center" onclick='showDetial("${file_name}")' >
  135. ${file_name}
  136. <span class="badge badge-primary badge-pill">${file_count}</span>
  137. </li>`;
  138. }
  139. function showVul() {
  140. let vul_data = [];
  141. let i = 0;
  142. let keys = vulTypeCount.getKeys();
  143. for(i = 0 ; i < vulTypeCount.getSize(); i++){
  144. vul_data.push({name: keys[i], y:vulTypeCount.getValue(keys[i]).length});
  145. }
  146. Highcharts.chart('container1', {
  147. chart: {
  148. plotBackgroundColor: null,
  149. plotBorderWidth: null,
  150. plotShadow: false,
  151. type: 'pie'
  152. },
  153. title: {
  154. text: 'Vulnerabilities Per Category'
  155. },
  156. tooltip: {
  157. pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
  158. },
  159. plotOptions: {
  160. pie: {
  161. allowPointSelect: true,
  162. cursor: 'pointer',
  163. dataLabels: {
  164. enabled: false
  165. },
  166. showInLegend: true
  167. }
  168. },
  169. series: [{
  170. name: 'Category',
  171. colorByPoint: true,
  172. data: vul_data
  173. }]
  174. });
  175. }
  176. function showFiles() {
  177. let vul_file = [];
  178. let vul_count = [];
  179. let i = 0;
  180. let keys = fileCount.getKeys();
  181. for(i = 0 ; i < fileCount.getSize(); i++){
  182. vul_file.push(keys[i]);
  183. vul_count.push(fileCount.getValue(keys[i]).length);
  184. }
  185. var chart = Highcharts.chart('container2', {
  186. chart: {
  187. zoomType: 'xy'
  188. },
  189. title: {
  190. text: 'Vulnerabilities Per File '
  191. },
  192. subtitle: {
  193. text: ' AliOS-Things '
  194. },
  195. xAxis: [{
  196. categories: vul_file,
  197. crosshair: true
  198. }],
  199. yAxis: [{ // Primary yAxis
  200. labels: {
  201. format: '{value} risks',
  202. style: {
  203. color: Highcharts.getOptions().colors[1]
  204. }
  205. },
  206. title: {
  207. text: 'Count',
  208. style: {
  209. color: Highcharts.getOptions().colors[1]
  210. }
  211. }
  212. }],
  213. tooltip: {
  214. shared: true
  215. },
  216. // legend: {
  217. // layout: 'vertical',
  218. // align: 'left',
  219. // verticalAlign: 'top',
  220. // y: 100,
  221. // floating: true,
  222. // backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
  223. // },
  224. series: [{
  225. name: 'Per File',
  226. type: 'column', /**spline */
  227. data: vul_count,
  228. tooltip: {
  229. valueSuffix: ' risks'
  230. }
  231. }]
  232. });
  233. }
  234. function showDetial(file_name){
  235. let vul = fileCount.getValue(file_name);
  236. let all_html = "";
  237. $("#modal-title").text(file_name + " Detail");
  238. for(i= 0 ; i < vul.length; i ++){
  239. all_html += ` In function ${vul[i].subroutine},<br> Line ${vul[i].lineno}, &nbsp;&nbsp;&nbsp;
  240. <span style="color:red"> ${vul[i].variate} </span> -
  241. <span style="color:orange"> ${trans(vul[i].vultype)}</span> <br><br> `;
  242. }
  243. $("#file-detial").html(all_html);
  244. $("#modal-detial").modal('show');
  245. }
  246. function trans(tp){
  247. if(tp == "NPD"){
  248. return "Null Pointer Dereference";
  249. }else if(tp == "UIV"){
  250. return "Uninitialized Variable";
  251. }
  252. }