makefsdata.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use 5.010;
  5. use FindBin '$Bin';
  6. use constant HTTPD_SERVER_AGENT => "lwIP/1.3.1 (http://savannah.nongnu.org/projects/lwip)";
  7. my @HTTPHeaderStrings =
  8. (
  9. "Content-type: text/html\r\nContent-Encoding: gzip\r\n",
  10. "Content-type: text/html\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n",
  11. "Content-type: image/gif\r\n",
  12. "Content-type: image/png\r\n",
  13. "Content-type: image/jpeg\r\n",
  14. "Content-type: image/bmp\r\n",
  15. "Content-type: image/x-icon\r\n",
  16. "Content-type: application/octet-stream\r\n",
  17. "Content-type: application/x-javascript\r\nContent-Encoding: gzip\r\n",
  18. "Content-type: application/x-javascript\r\nContent-Encoding: gzip\r\n",
  19. "Content-type: text/css\r\nContent-Encoding: gzip\r\n",
  20. "Content-type: application/x-shockwave-flash\r\n",
  21. "Content-type: text/xml\r\n",
  22. "Content-type: text/plain\r\n",
  23. "HTTP/1.0 200 OK\r\n",
  24. "HTTP/1.0 404 File not found\r\n",
  25. "HTTP/1.0 400 Bad Request\r\n",
  26. "HTTP/1.0 501 Not Implemented\r\n",
  27. "HTTP/1.1 200 OK\r\n",
  28. "HTTP/1.1 404 File not found\r\n",
  29. "HTTP/1.1 400 Bad Request\r\n",
  30. "HTTP/1.1 501 Not Implemented\r\n",
  31. "Content-Length: ",
  32. "Connection: Close\r\n",
  33. "Server: ".HTTPD_SERVER_AGENT."\r\n",
  34. "\r\n<html><body><h2>404: The requested file cannot be found.</h2></body></html>\r\n"
  35. );
  36. use constant {
  37. HTTP_HDR_HTML => 0, # text/html
  38. HTTP_HDR_SSI => 1, # text/html Expires...
  39. HTTP_HDR_GIF => 2, # image/gif
  40. HTTP_HDR_PNG => 3, # image/png
  41. HTTP_HDR_JPG => 4, # image/jpeg
  42. HTTP_HDR_BMP => 5, # image/bmp
  43. HTTP_HDR_ICO => 6, # image/x-icon
  44. HTTP_HDR_APP => 7, # application/octet-stream
  45. HTTP_HDR_JS => 8, # application/x-javascript
  46. HTTP_HDR_RA => 9, # application/x-javascript
  47. HTTP_HDR_CSS => 10, # text/css
  48. HTTP_HDR_SWF => 11, # application/x-shockwave-flash
  49. HTTP_HDR_XML => 12, # text/xml
  50. HTTP_HDR_DEFAULT_TYPE => 13, # text/plain
  51. HTTP_HDR_OK => 14, # 200 OK
  52. HTTP_HDR_NOT_FOUND => 15, # 404 File not found
  53. HTTP_HDR_BAD_REQUEST => 16, # 400 Bad request
  54. HTTP_HDR_NOT_IMPL => 17, # 501 Not Implemented
  55. HTTP_HDR_OK_11 => 18, # 200 OK
  56. HTTP_HDR_NOT_FOUND_11 => 19, # 404 File not found
  57. HTTP_HDR_BAD_REQUEST_11 => 20, # 400 Bad request
  58. HTTP_HDR_NOT_IMPL_11 => 21, # 501 Not Implemented
  59. HTTP_HDR_CONTENT_LENGTH => 22, # Content-Length: (HTTP 1.1)
  60. HTTP_HDR_CONN_CLOSE => 23, # Connection: Close (HTTP 1.1)
  61. HTTP_HDR_SERVER => 24, # Server: HTTPD_SERVER_AGENT
  62. DEFAULT_404_HTML => 25, # default 404 body
  63. };
  64. use constant HEX_BYTES_PER_LINE => 16;
  65. my $cwd;
  66. my $fsdir;
  67. my $num_args = $#ARGV + 1;
  68. if ($num_args > 0) {
  69. foreach my $argnum (0 .. $#ARGV) {
  70. if ($ARGV[$argnum] eq "-h" or $ARGV[$argnum] eq "--help"){
  71. say " __ ___ __ ____ __ __ ";
  72. say " / |/ /___ _/ /_____ / __/________/ /___ _/ /_____ _";
  73. say " / /|_/ / __ `/ //_/ _ \\/ /_/ ___/ __ / __ `/ __/ __ `/";
  74. say " / / / / /_/ / ,< / __/ __(__ ) /_/ / /_/ / /_/ /_/ / ";
  75. say "/_/ /_/\\__,_/_/|_|\\___/_/ /____/\\__,_/\\__,_/\\__/\\__,_/ ";
  76. say " ";
  77. say "makefsdata - HTML to C source converter\n\n";
  78. say "Usage: makefsdata.pl <fsdata_file_dir> <fs_folder_dir>\n";
  79. exit;
  80. }
  81. }
  82. if($num_args != 2) {
  83. say "Incorrect arguments!\n";
  84. say "Usage: makefsdata.pl <fsdata_file_dir> <fs_folder_dir>\n";
  85. exit;
  86. }
  87. if(-d $ARGV[0] and -d $ARGV[1] ){
  88. # say $ARGV[0];
  89. # say $ARGV[1];
  90. $cwd = $ARGV[0];
  91. $fsdir = $ARGV[1];
  92. } else {
  93. say "Drectory dosn't exist!\n";
  94. exit;
  95. }
  96. } else {
  97. $cwd = $Bin;
  98. $fsdir = "$cwd/fs";
  99. }
  100. open(OUTPUT, "> $cwd/fsdata.c");
  101. chdir($fsdir);
  102. my $dir = '.';
  103. opendir(DIR, $dir) or die "Can't open directory $!";
  104. my $file;
  105. my $fvar;
  106. my $prevfile;
  107. my @files;
  108. my @fvars;
  109. print(OUTPUT "#include \"lwip/def.h\"\n");
  110. print(OUTPUT "#include \"fsdata.h\"\n");
  111. print(OUTPUT "\n\n#define file_NULL (struct fsdata_file *) NULL\n\n\n");
  112. my $z = 0;
  113. while($file = readdir(DIR)) {
  114. # Do not include files in CVS directories nor backup files.
  115. if($file =~ /(CVS|~)/) {
  116. next;
  117. }
  118. next if ($file =~ m/^\./);
  119. say("Processing --> $file");
  120. my $size = -s $file;
  121. my $curHeader;
  122. if($file =~ /\.html$/) {
  123. $curHeader = $HTTPHeaderStrings[HTTP_HDR_HTML];
  124. } elsif($file =~ /\.js$/) {
  125. $curHeader = $HTTPHeaderStrings[HTTP_HDR_JS];
  126. } elsif($file =~ /\.css$/) {
  127. $curHeader = $HTTPHeaderStrings[HTTP_HDR_CSS];
  128. } elsif($file =~ /\.ico$/) {
  129. $curHeader = $HTTPHeaderStrings[HTTP_HDR_ICO];
  130. } elsif($file =~ /\.gif$/) {
  131. $curHeader = $HTTPHeaderStrings[HTTP_HDR_GIF];
  132. } elsif($file =~ /\.png$/) {
  133. $curHeader = $HTTPHeaderStrings[HTTP_HDR_PNG];
  134. } elsif($file =~ /\.jpg$/) {
  135. $curHeader = $HTTPHeaderStrings[HTTP_HDR_JPG];
  136. } elsif($file =~ /\.class$/) {
  137. $curHeader = $HTTPHeaderStrings[HTTP_HDR_APP];
  138. } else {
  139. $curHeader = $HTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE];
  140. }
  141. my $contentLength = "$HTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH]$size\r\n";
  142. say $curHeader;
  143. say "$HTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH]$size";
  144. system("cp $file /tmp/file");
  145. open(FILE, "/tmp/file");
  146. unlink("/tmp/file");
  147. my $i = 0;
  148. my $fvar = $file;
  149. $fvar =~ s-/-_-g;
  150. $fvar =~ s-\.-_-g;
  151. my $filenamelen = length("/$file");
  152. print(OUTPUT "static const unsigned int dummy_align__$fvar = $z;\n");
  153. print(OUTPUT "static const unsigned char data__".$fvar."[] = {\n");
  154. print(OUTPUT "/* /$file ($filenamelen chars) */\n");
  155. # Filename to hex
  156. for(my $j = 0; $j < $filenamelen; $j++) {
  157. printf(OUTPUT "0x%02.2x,", unpack("C", substr("/$file", $j, 1)));
  158. }
  159. printf(OUTPUT "0x00,\n");
  160. $z++;
  161. print(OUTPUT "\n/* HTTP header */\n");
  162. sub hearderGen{
  163. my ($header) = @_;
  164. my $header_len = length($header);
  165. print(OUTPUT "\n/* \"$header\" ($header_len bytes) */");
  166. for(my $j = 0; $j < length($header); $j++) {
  167. if(($j % HEX_BYTES_PER_LINE) == 0) {
  168. print(OUTPUT "\n");
  169. }
  170. printf(OUTPUT "0x%02.2x,", unpack("C", substr($header, $j, 1)));
  171. }
  172. return;
  173. }
  174. # Generate random Etag sting
  175. sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] }
  176. my $etag = rndStr(15, 'A'..'Z', 0..9, 'a'..'z');
  177. # Put hex heders to file
  178. if($file =~ /404/) {
  179. hearderGen($HTTPHeaderStrings[HTTP_HDR_NOT_FOUND_11]);
  180. } else {
  181. hearderGen($HTTPHeaderStrings[HTTP_HDR_OK_11]);
  182. }
  183. hearderGen($HTTPHeaderStrings[HTTP_HDR_SERVER]);
  184. hearderGen($contentLength);
  185. hearderGen($HTTPHeaderStrings[HTTP_HDR_CONN_CLOSE]);
  186. hearderGen($curHeader);
  187. hearderGen("ETag: \"$etag\"\r\n");
  188. # /r/n
  189. printf(OUTPUT "0x0d,0x0a,\n");
  190. say "Etag: \"$etag\"";
  191. say "----------------------------\n";
  192. # RAW file to hex
  193. print(OUTPUT "\n/* raw file data ($size bytes) */");
  194. while(read(FILE, my $data, 1)) {
  195. if($i == 0) {
  196. print(OUTPUT "\n");
  197. }
  198. printf(OUTPUT "0x%02.2x,", unpack("C", $data));
  199. $i++;
  200. if($i == HEX_BYTES_PER_LINE) {
  201. print(OUTPUT "");
  202. $i = 0;
  203. }
  204. }
  205. print(OUTPUT "};\n\n");
  206. close(FILE);
  207. push(@fvars, $fvar);
  208. push(@files, $file);
  209. }
  210. my $i;
  211. for($i = 0; $i < @fvars; $i++) {
  212. $file = $files[$i];
  213. $fvar = $fvars[$i];
  214. if($i == 0) {
  215. $prevfile = "file_NULL";
  216. } else {
  217. $prevfile = "file__" . $fvars[$i - 1];
  218. }
  219. print(OUTPUT "const struct fsdata_file file__".$fvar."[] = {{\n\t$prevfile,\n\tdata__$fvar,\n\t");
  220. print(OUTPUT "data__$fvar + ". (length($file) + 2) .",\n\t");
  221. print(OUTPUT "sizeof(data__$fvar) - ". (length($file) + 2) .",\n\t1,\n}};\n\n");
  222. }
  223. print(OUTPUT "#define FS_ROOT file__$fvars[$i - 1]\n");
  224. print(OUTPUT "#define FS_NUMFILES $i\n");
  225. say "Done!\nNow you have fsdata.c";