STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
makefsdata.c
Go to the documentation of this file.
1 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #ifdef WIN32
17 #define WIN32_LEAN_AND_MEAN
18 #include "windows.h"
19 #else
20 #include <dir.h>
21 #endif
22 #include <dos.h>
23 #include <string.h>
24 
25 /* Compatibility defines Win32 vs. DOS */
26 #ifdef WIN32
27 
28 #define FIND_T WIN32_FIND_DATAA
29 #define FIND_T_FILENAME(fInfo) (fInfo.cFileName)
30 #define FIND_T_IS_DIR(fInfo) ((fInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
31 #define FIND_T_IS_FILE(fInfo) ((fInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
32 #define FIND_RET_T HANDLE
33 #define FINDFIRST_FILE(path, result) FindFirstFileA(path, result)
34 #define FINDFIRST_DIR(path, result) FindFirstFileA(path, result)
35 #define FINDNEXT(ff_res, result) FindNextFileA(ff_res, result)
36 #define FINDFIRST_SUCCEEDED(ret) (ret != INVALID_HANDLE_VALUE)
37 #define FINDNEXT_SUCCEEDED(ret) (ret == TRUE)
38 
39 #define GETCWD(path, len) GetCurrentDirectoryA(len, path)
40 #define CHDIR(path) SetCurrentDirectoryA(path)
41 #define CHDIR_SUCCEEDED(ret) (ret == TRUE)
42 
43 #else
44 
45 #define FIND_T struct ffblk
46 #define FIND_T_FILENAME(fInfo) (fInfo.ff_name)
47 #define FIND_T_IS_DIR(fInfo) ((fInfo.ff_attrib & FA_DIREC) == FA_DIREC)
48 #define FIND_T_IS_FILE(fInfo) (1)
49 #define FIND_RET_T int
50 #define FINDFIRST_FILE(path, result) findfirst(path, result, FA_ARCH)
51 #define FINDFIRST_DIR(path, result) findfirst(path, result, FA_DIREC)
52 #define FINDNEXT(ff_res, result) FindNextFileA(ff_res, result)
53 #define FINDFIRST_SUCCEEDED(ret) (ret == 0)
54 #define FINDNEXT_SUCCEEDED(ret) (ret == 0)
55 
56 #define GETCWD(path, len) getcwd(path, len)
57 #define CHDIR(path) chdir(path)
58 #define CHDIR_SUCCEEDED(ret) (ret == 0)
59 
60 #endif
61 
62 #define NEWLINE "\r\n"
63 #define NEWLINE_LEN 2
64 
65 /* define this to get the header variables we use to build HTTP headers */
66 #define LWIP_HTTPD_DYNAMIC_HEADERS 1
67 #define LWIP_HTTPD_SSI 1
68 #include "../httpd_structs.h"
69 
70 #include "../core/inet_chksum.c"
71 #include "../core/def.c"
72 
74 const char *serverID = "Server: "HTTPD_SERVER_AGENT"\r\n";
75 
76 /* change this to suit your MEM_ALIGNMENT */
77 #define PAYLOAD_ALIGNMENT 4
78 /* set this to 0 to prevent aligning payload */
79 #define ALIGN_PAYLOAD 1
80 /* define this to a type that has the required alignment */
81 #define PAYLOAD_ALIGN_TYPE "unsigned int"
82 static int payload_alingment_dummy_counter = 0;
83 
84 #define HEX_BYTES_PER_LINE 16
85 
86 #define MAX_PATH_LEN 256
87 
88 #define COPY_BUFSIZE 10240
89 
90 struct file_entry
91 {
92  struct file_entry* next;
93  const char* filename_c;
94 };
95 
96 int process_sub(FILE *data_file, FILE *struct_file);
97 int process_file(FILE *data_file, FILE *struct_file, const char *filename);
98 int file_write_http_header(FILE *data_file, const char *filename, int file_size,
99  u16_t *http_hdr_len, u16_t *http_hdr_chksum);
100 int file_put_ascii(FILE *file, const char *ascii_string, int len, int *i);
101 int s_put_ascii(char *buf, const char *ascii_string, int len, int *i);
102 void concat_files(const char *file1, const char *file2, const char *targetfile);
103 int check_path(char* path, size_t size);
104 
105 static unsigned char file_buffer_raw[COPY_BUFSIZE];
106 /* 5 bytes per char + 3 bytes per line */
107 static char file_buffer_c[COPY_BUFSIZE * 5 + ((COPY_BUFSIZE / HEX_BYTES_PER_LINE) * 3)];
108 
111 char hdr_buf[4096];
112 
113 unsigned char processSubs = 1;
114 unsigned char includeHttpHeader = 1;
115 unsigned char useHttp11 = 0;
116 unsigned char supportSsi = 1;
117 unsigned char precalcChksum = 0;
118 
121 
122 static void print_usage(void)
123 {
124  printf(" Usage: htmlgen [targetdir] [-s] [-i] [-f:<filename>]" NEWLINE NEWLINE);
125  printf(" targetdir: relative or absolute path to files to convert" NEWLINE);
126  printf(" switch -s: toggle processing of subdirectories (default is on)" NEWLINE);
127  printf(" switch -e: exclude HTTP header from file (header is created at runtime, default is off)" NEWLINE);
128  printf(" switch -11: include HTTP 1.1 header (1.0 is default)" NEWLINE);
129  printf(" switch -nossi: no support for SSI (cannot calculate Content-Length for SSI)" NEWLINE);
130  printf(" switch -c: precalculate checksums for all pages (default is off)" NEWLINE);
131  printf(" switch -f: target filename (default is \"fsdata.c\")" NEWLINE);
132  printf(" if targetdir not specified, htmlgen will attempt to" NEWLINE);
133  printf(" process files in subdirectory 'fs'" NEWLINE);
134 }
135 
136 int main(int argc, char *argv[])
137 {
138  char path[MAX_PATH_LEN];
139  char appPath[MAX_PATH_LEN];
140  FILE *data_file;
141  FILE *struct_file;
142  int filesProcessed;
143  int i;
144  char targetfile[MAX_PATH_LEN];
145  strcpy(targetfile, "fsdata.c");
146 
147  memset(path, 0, sizeof(path));
148  memset(appPath, 0, sizeof(appPath));
149 
150  printf(NEWLINE " makefsdata - HTML to C source converter" NEWLINE);
151  printf(" by Jim Pettinato - circa 2003 " NEWLINE);
152  printf(" extended by Simon Goldschmidt - 2009 " NEWLINE NEWLINE);
153 
154  strcpy(path, "fs");
155  for(i = 1; i < argc; i++) {
156  if (argv[i] == NULL) {
157  continue;
158  }
159  if (argv[i][0] == '-') {
160  if (strstr(argv[i], "-s")) {
161  processSubs = 0;
162  } else if (strstr(argv[i], "-e")) {
163  includeHttpHeader = 0;
164  } else if (strstr(argv[i], "-11")) {
165  useHttp11 = 1;
166  } else if (strstr(argv[i], "-nossi")) {
167  supportSsi = 0;
168  } else if (strstr(argv[i], "-c")) {
169  precalcChksum = 1;
170  } else if((argv[i][1] == 'f') && (argv[i][2] == ':')) {
171  strncpy(targetfile, &argv[i][3], sizeof(targetfile) - 1);
172  targetfile[sizeof(targetfile) - 1] = 0;
173  printf("Writing to file \"%s\"\n", targetfile);
174  } else if ((strstr(argv[i], "-?")) || (strstr(argv[i], "-h"))) {
175  print_usage();
176  exit(0);
177  }
178  } else if ((argv[i][0] == '/') && (argv[i][1] == '?') && (argv[i][2] == 0)) {
179  print_usage();
180  exit(0);
181  } else {
182  strncpy(path, argv[i], sizeof(path)-1);
183  path[sizeof(path)-1] = 0;
184  }
185  }
186 
187  if(!check_path(path, sizeof(path))) {
188  printf("Invalid path: \"%s\"." NEWLINE, path);
189  exit(-1);
190  }
191 
192  GETCWD(appPath, MAX_PATH_LEN);
193  /* if command line param or subdir named 'fs' not found spout usage verbiage */
194  if (!CHDIR_SUCCEEDED(CHDIR(path))) {
195  /* if no subdir named 'fs' (or the one which was given) exists, spout usage verbiage */
196  printf(" Failed to open directory \"%s\"." NEWLINE NEWLINE, path);
197  print_usage();
198  exit(-1);
199  }
200  CHDIR(appPath);
201 
202  printf("HTTP %sheader will %s statically included." NEWLINE,
203  (includeHttpHeader ? (useHttp11 ? "1.1 " : "1.0 ") : ""),
204  (includeHttpHeader ? "be" : "not be"));
205 
206  sprintf(curSubdir, ""); /* start off in web page's root directory - relative paths */
207  printf(" Processing all files in directory %s", path);
208  if (processSubs) {
209  printf(" and subdirectories..." NEWLINE NEWLINE);
210  } else {
211  printf("..." NEWLINE NEWLINE);
212  }
213 
214  data_file = fopen("fsdata.tmp", "wb");
215  if (data_file == NULL) {
216  printf("Failed to create file \"fsdata.tmp\"\n");
217  exit(-1);
218  }
219  struct_file = fopen("fshdr.tmp", "wb");
220  if (struct_file == NULL) {
221  printf("Failed to create file \"fshdr.tmp\"\n");
222  fclose(data_file);
223  exit(-1);
224  }
225 
226  CHDIR(path);
227 
228  fprintf(data_file, "#include \"lwip/apps/fs.h\"" NEWLINE);
229  fprintf(data_file, "#include \"lwip/def.h\"" NEWLINE);
230  fprintf(data_file, "#include \"fsdata.h\"" NEWLINE NEWLINE NEWLINE);
231 
232  fprintf(data_file, "#define file_NULL (struct fsdata_file *) NULL" NEWLINE NEWLINE NEWLINE);
233 
234  sprintf(lastFileVar, "NULL");
235 
236  filesProcessed = process_sub(data_file, struct_file);
237 
238  /* data_file now contains all of the raw data.. now append linked list of
239  * file header structs to allow embedded app to search for a file name */
240  fprintf(data_file, NEWLINE NEWLINE);
241  fprintf(struct_file, "#define FS_ROOT file_%s" NEWLINE, lastFileVar);
242  fprintf(struct_file, "#define FS_NUMFILES %d" NEWLINE NEWLINE, filesProcessed);
243 
244  fclose(data_file);
245  fclose(struct_file);
246 
247  CHDIR(appPath);
248  /* append struct_file to data_file */
249  printf(NEWLINE "Creating target file..." NEWLINE NEWLINE);
250  concat_files("fsdata.tmp", "fshdr.tmp", targetfile);
251 
252  /* if succeeded, delete the temporary files */
253  if (remove("fsdata.tmp") != 0) {
254  printf("Warning: failed to delete fsdata.tmp\n");
255  }
256  if (remove("fshdr.tmp") != 0) {
257  printf("Warning: failed to delete fshdr.tmp\n");
258  }
259 
260  printf(NEWLINE "Processed %d files - done." NEWLINE NEWLINE, filesProcessed);
261 
262  while (first_file != NULL) {
263  struct file_entry* fe = first_file;
264  first_file = fe->next;
265  free(fe);
266  }
267 
268  return 0;
269 }
270 
271 int check_path(char* path, size_t size)
272 {
273  size_t slen;
274  if (path[0] == 0) {
275  /* empty */
276  return 0;
277  }
278  slen = strlen(path);
279  if (slen >= size) {
280  /* not NULL-terminated */
281  return 0;
282  }
283  while ((slen > 0) && ((path[slen] == '\\') || (path[slen] == '/'))) {
284  /* path should not end with trailing backslash */
285  path[slen] = 0;
286  slen--;
287  }
288  if (slen == 0) {
289  return 0;
290  }
291  return 1;
292 }
293 
294 static void copy_file(const char *filename_in, FILE *fout)
295 {
296  FILE *fin;
297  size_t len;
298  fin = fopen(filename_in, "rb");
299  if (fin == NULL) {
300  printf("Failed to open file \"%s\"\n", filename_in);
301  exit(-1);
302  }
303 
304  while((len = fread(file_buffer_raw, 1, COPY_BUFSIZE, fin)) > 0)
305  {
306  fwrite(file_buffer_raw, 1, len, fout);
307  }
308  fclose(fin);
309 }
310 
311 void concat_files(const char *file1, const char *file2, const char *targetfile)
312 {
313  FILE *fout;
314  fout = fopen(targetfile, "wb");
315  if (fout == NULL) {
316  printf("Failed to open file \"%s\"\n", targetfile);
317  exit(-1);
318  }
319  copy_file(file1, fout);
320  copy_file(file2, fout);
321  fclose(fout);
322 }
323 
324 int process_sub(FILE *data_file, FILE *struct_file)
325 {
326  FIND_T fInfo;
327  FIND_RET_T fret;
328  int filesProcessed = 0;
329 
330  if (processSubs) {
331  /* process subs recursively */
332  size_t sublen = strlen(curSubdir);
333  size_t freelen = sizeof(curSubdir) - sublen - 1;
334  LWIP_ASSERT("sublen < sizeof(curSubdir)", sublen < sizeof(curSubdir));
335  fret = FINDFIRST_DIR("*", &fInfo);
336  if (FINDFIRST_SUCCEEDED(fret)) {
337  do {
338  const char *curName = FIND_T_FILENAME(fInfo);
339  if ((curName[0] == '.') || (strcmp(curName, "CVS") == 0)) {
340  continue;
341  }
342  if (!FIND_T_IS_DIR(fInfo)) {
343  continue;
344  }
345  if (freelen > 0) {
346  CHDIR(curName);
347  strncat(curSubdir, "/", freelen);
348  strncat(curSubdir, curName, freelen - 1);
349  curSubdir[sizeof(curSubdir) - 1] = 0;
350  printf(NEWLINE "processing subdirectory %s/..." NEWLINE, curSubdir);
351  filesProcessed += process_sub(data_file, struct_file);
352  CHDIR("..");
353  curSubdir[sublen] = 0;
354  } else {
355  printf("WARNING: cannot process sub due to path length restrictions: \"%s/%s\"\n", curSubdir, curName);
356  }
357  } while (FINDNEXT_SUCCEEDED(FINDNEXT(fret, &fInfo)));
358  }
359  }
360 
361  fret = FINDFIRST_FILE("*.*", &fInfo);
362  if (FINDFIRST_SUCCEEDED(fret)) {
363  /* at least one file in directory */
364  do {
365  if (FIND_T_IS_FILE(fInfo)) {
366  const char *curName = FIND_T_FILENAME(fInfo);
367  printf("processing %s/%s..." NEWLINE, curSubdir, curName);
368  if (process_file(data_file, struct_file, curName) < 0) {
369  printf(NEWLINE "Error... aborting" NEWLINE);
370  return -1;
371  }
372  filesProcessed++;
373  }
374  } while (FINDNEXT_SUCCEEDED(FINDNEXT(fret, &fInfo)));
375  }
376  return filesProcessed;
377 }
378 
379 int get_file_size(const char* filename)
380 {
381  FILE *inFile;
382  int file_size = -1;
383  inFile = fopen(filename, "rb");
384  if (inFile == NULL) {
385  printf("Failed to open file \"%s\"\n", filename);
386  exit(-1);
387  }
388  fseek(inFile, 0, SEEK_END);
389  file_size = ftell(inFile);
390  fclose(inFile);
391  return file_size;
392 }
393 
394 void process_file_data(const char *filename, FILE *data_file)
395 {
396  FILE *source_file;
397  size_t len, written, i, src_off=0;
398 
399  source_file = fopen(filename, "rb");
400  if (source_file == NULL) {
401  printf("Failed to open file \"%s\"\n", filename);
402  exit(-1);
403  }
404 
405  do {
406  size_t off = 0;
407  len = fread(file_buffer_raw, 1, COPY_BUFSIZE, source_file);
408  if (len > 0) {
409  for (i = 0; i < len; i++) {
410  sprintf(&file_buffer_c[off], "0x%02.2x,", file_buffer_raw[i]);
411  off += 5;
412  if ((++src_off % HEX_BYTES_PER_LINE) == 0) {
413  memcpy(&file_buffer_c[off], NEWLINE, NEWLINE_LEN);
414  off += NEWLINE_LEN;
415  }
416  }
417  written = fwrite(file_buffer_c, 1, off, data_file);
418  }
419  } while(len > 0);
420  fclose(source_file);
421 }
422 
423 int write_checksums(FILE *struct_file, const char *filename, const char *varname,
424  u16_t hdr_len, u16_t hdr_chksum)
425 {
426  int chunk_size = TCP_MSS;
427  int offset;
428  size_t len;
429  int i = 0;
430  FILE *f;
431 #if LWIP_TCP_TIMESTAMPS
432  /* when timestamps are used, usable space is 12 bytes less per segment */
433  chunk_size -= 12;
434 #endif
435 
436  fprintf(struct_file, "#if HTTPD_PRECALCULATED_CHECKSUM" NEWLINE);
437  fprintf(struct_file, "const struct fsdata_chksum chksums_%s[] = {" NEWLINE, varname);
438 
439  memset(file_buffer_raw, 0xab, sizeof(file_buffer_raw));
440  f = fopen(filename, "rb");
441  if (f == NULL) {
442  printf("Failed to open file \"%s\"\n", filename);
443  exit(-1);
444  }
445  if (hdr_len > 0) {
446  /* add checksum for HTTP header */
447  fprintf(struct_file, "{%d, 0x%04x, %d}," NEWLINE, 0, hdr_chksum, hdr_len);
448  i++;
449  }
450  for (offset = hdr_len; ; offset += len) {
451  unsigned short chksum;
452  len = fread(file_buffer_raw, 1, chunk_size, f);
453  if (len == 0) {
454  break;
455  }
456  chksum = ~inet_chksum(file_buffer_raw, (u16_t)len);
457  /* add checksum for data */
458  fprintf(struct_file, "{%d, 0x%04x, %d}," NEWLINE, offset, chksum, len);
459  i++;
460  }
461  fclose(f);
462  fprintf(struct_file, "};" NEWLINE);
463  fprintf(struct_file, "#endif /* HTTPD_PRECALCULATED_CHECKSUM */" NEWLINE);
464  return i;
465 }
466 
467 static int is_valid_char_for_c_var(char x)
468 {
469  if (((x >= 'A') && (x <= 'Z')) ||
470  ((x >= 'a') && (x <= 'z')) ||
471  ((x >= '0') && (x <= '9')) ||
472  (x == '_'))
473  {
474  return 1;
475  }
476  return 0;
477 }
478 
479 static void fix_filename_for_c(char* qualifiedName, size_t max_len)
480 {
481  struct file_entry* f;
482  size_t len = strlen(qualifiedName);
483  char *new_name = malloc(len + 2);
484  int filename_ok;
485  int cnt = 0;
486  size_t i;
487  if (len + 3 == max_len) {
488  printf("File name too long: \"%s\"\n", qualifiedName);
489  exit(-1);
490  }
491  strcpy(new_name, qualifiedName);
492  for (i = 0; i < len; i++) {
493  if (!is_valid_char_for_c_var(new_name[i])) {
494  new_name[i] = '_';
495  }
496  }
497  do {
498  filename_ok = 1;
499  for (f = first_file; f != NULL; f = f->next) {
500  if (!strcmp(f->filename_c, new_name)) {
501  filename_ok = 0;
502  cnt++;
503  /* try next unique file name */
504  sprintf(&new_name[len], "%d", cnt);
505  break;
506  }
507  }
508  } while (!filename_ok && (cnt < 999));
509  if (!filename_ok) {
510  printf("Failed to get unique file name: \"%s\"\n", qualifiedName);
511  exit(-1);
512  }
513  strcpy(qualifiedName, new_name);
514  free(new_name);
515 }
516 
517 static void register_filename(const char* qualifiedName)
518 {
519  struct file_entry* fe = malloc(sizeof(struct file_entry));
520  fe->filename_c = strdup(qualifiedName);
521  fe->next = NULL;
522  if (first_file == NULL) {
523  first_file = last_file = fe;
524  } else {
525  last_file->next = fe;
526  last_file = fe;
527  }
528 }
529 
530 int process_file(FILE *data_file, FILE *struct_file, const char *filename)
531 {
532  char varname[MAX_PATH_LEN];
533  int i = 0;
534  char qualifiedName[MAX_PATH_LEN];
535  int file_size;
536  u16_t http_hdr_chksum = 0;
537  u16_t http_hdr_len = 0;
538  int chksum_count = 0;
539 
540  /* create qualified name (TODO: prepend slash or not?) */
541  sprintf(qualifiedName,"%s/%s", curSubdir, filename);
542  /* create C variable name */
543  strcpy(varname, qualifiedName);
544  /* convert slashes & dots to underscores */
545  fix_filename_for_c(varname, MAX_PATH_LEN);
546  register_filename(varname);
547 #if ALIGN_PAYLOAD
548  /* to force even alignment of array */
549  fprintf(data_file, "static const " PAYLOAD_ALIGN_TYPE " dummy_align_%s = %d;" NEWLINE, varname, payload_alingment_dummy_counter++);
550 #endif /* ALIGN_PAYLOAD */
551  fprintf(data_file, "static const unsigned char data_%s[] = {" NEWLINE, varname);
552  /* encode source file name (used by file system, not returned to browser) */
553  fprintf(data_file, "/* %s (%d chars) */" NEWLINE, qualifiedName, strlen(qualifiedName)+1);
554  file_put_ascii(data_file, qualifiedName, strlen(qualifiedName)+1, &i);
555 #if ALIGN_PAYLOAD
556  /* pad to even number of bytes to assure payload is on aligned boundary */
557  while(i % PAYLOAD_ALIGNMENT != 0) {
558  fprintf(data_file, "0x%02.2x,", 0);
559  i++;
560  }
561 #endif /* ALIGN_PAYLOAD */
562  fprintf(data_file, NEWLINE);
563 
564  file_size = get_file_size(filename);
565  if (includeHttpHeader) {
566  file_write_http_header(data_file, filename, file_size, &http_hdr_len, &http_hdr_chksum);
567  }
568  if (precalcChksum) {
569  chksum_count = write_checksums(struct_file, filename, varname, http_hdr_len, http_hdr_chksum);
570  }
571 
572  /* build declaration of struct fsdata_file in temp file */
573  fprintf(struct_file, "const struct fsdata_file file_%s[] = { {" NEWLINE, varname);
574  fprintf(struct_file, "file_%s," NEWLINE, lastFileVar);
575  fprintf(struct_file, "data_%s," NEWLINE, varname);
576  fprintf(struct_file, "data_%s + %d," NEWLINE, varname, i);
577  fprintf(struct_file, "sizeof(data_%s) - %d," NEWLINE, varname, i);
578  fprintf(struct_file, "%d," NEWLINE, includeHttpHeader);
579  if (precalcChksum) {
580  fprintf(struct_file, "#if HTTPD_PRECALCULATED_CHECKSUM" NEWLINE);
581  fprintf(struct_file, "%d, chksums_%s," NEWLINE, chksum_count, varname);
582  fprintf(struct_file, "#endif /* HTTPD_PRECALCULATED_CHECKSUM */" NEWLINE);
583  }
584  fprintf(struct_file, "}};" NEWLINE NEWLINE);
585  strcpy(lastFileVar, varname);
586 
587  /* write actual file contents */
588  i = 0;
589  fprintf(data_file, NEWLINE "/* raw file data (%d bytes) */" NEWLINE, file_size);
590  process_file_data(filename, data_file);
591  fprintf(data_file, "};" NEWLINE NEWLINE);
592 
593  return 0;
594 }
595 
596 int file_write_http_header(FILE *data_file, const char *filename, int file_size,
597  u16_t *http_hdr_len, u16_t *http_hdr_chksum)
598 {
599  int i = 0;
600  int response_type = HTTP_HDR_OK;
601  const char* file_type;
602  const char *cur_string;
603  size_t cur_len;
604  int written = 0;
605  size_t hdr_len = 0;
606  u16_t acc;
607  const char *file_ext;
608  int j;
609  u8_t keepalive = useHttp11;
610 
611  if (keepalive) {
612  size_t loop;
613  for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
614  if (strstr(filename, g_pcSSIExtensions[loop])) {
615  /* no keepalive connection for SSI files */
616  keepalive = 0;
617  }
618  }
619  }
620 
621  memset(hdr_buf, 0, sizeof(hdr_buf));
622 
623  if (useHttp11) {
624  response_type = HTTP_HDR_OK_11;
625  }
626 
627  fprintf(data_file, NEWLINE "/* HTTP header */");
628  if (strstr(filename, "404") == filename) {
629  response_type = HTTP_HDR_NOT_FOUND;
630  if (useHttp11) {
631  response_type = HTTP_HDR_NOT_FOUND_11;
632  }
633  } else if (strstr(filename, "400") == filename) {
634  response_type = HTTP_HDR_BAD_REQUEST;
635  if (useHttp11) {
636  response_type = HTTP_HDR_BAD_REQUEST_11;
637  }
638  } else if (strstr(filename, "501") == filename) {
639  response_type = HTTP_HDR_NOT_IMPL;
640  if (useHttp11) {
641  response_type = HTTP_HDR_NOT_IMPL_11;
642  }
643  }
644  cur_string = g_psHTTPHeaderStrings[response_type];
645  cur_len = strlen(cur_string);
646  fprintf(data_file, NEWLINE "/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
647  written += file_put_ascii(data_file, cur_string, cur_len, &i);
648  i = 0;
649  if (precalcChksum) {
650  memcpy(&hdr_buf[hdr_len], cur_string, cur_len);
651  hdr_len += cur_len;
652  }
653 
654  cur_string = serverID;
655  cur_len = strlen(cur_string);
656  fprintf(data_file, NEWLINE "/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
657  written += file_put_ascii(data_file, cur_string, cur_len, &i);
658  i = 0;
659  if (precalcChksum) {
660  memcpy(&hdr_buf[hdr_len], cur_string, cur_len);
661  hdr_len += cur_len;
662  }
663 
664  file_ext = filename;
665  if (file_ext != NULL) {
666  while(strstr(file_ext, ".") != NULL) {
667  file_ext = strstr(file_ext, ".");
668  file_ext++;
669  }
670  }
671  if((file_ext == NULL) || (*file_ext == 0)) {
672  printf("failed to get extension for file \"%s\", using default.\n", filename);
673  file_type = HTTP_HDR_DEFAULT_TYPE;
674  } else {
675  file_type = NULL;
676  for(j = 0; j < NUM_HTTP_HEADERS; j++) {
677  if(!strcmp(file_ext, g_psHTTPHeaders[j].extension)) {
678  file_type = g_psHTTPHeaders[j].content_type;
679  break;
680  }
681  }
682  if (file_type == NULL) {
683  printf("failed to get file type for extension \"%s\", using default.\n", file_ext);
684  file_type = HTTP_HDR_DEFAULT_TYPE;
685  }
686  }
687 
688  if (useHttp11) {
689  char intbuf[MAX_PATH_LEN];
690  int content_len = file_size;
691  memset(intbuf, 0, sizeof(intbuf));
692 
693  if (!keepalive) {
694  content_len *= 2;
695  }
696  {
697  cur_string = g_psHTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH];
698  cur_len = strlen(cur_string);
699  fprintf(data_file, NEWLINE "/* \"%s%d\r\n\" (%d+ bytes) */" NEWLINE, cur_string, content_len, cur_len+2);
700  written += file_put_ascii(data_file, cur_string, cur_len, &i);
701  if (precalcChksum) {
702  memcpy(&hdr_buf[hdr_len], cur_string, cur_len);
703  hdr_len += cur_len;
704  }
705 
706  _itoa(content_len, intbuf, 10);
707  strcat(intbuf, "\r\n");
708  cur_len = strlen(intbuf);
709  written += file_put_ascii(data_file, intbuf, cur_len, &i);
710  i = 0;
711  if (precalcChksum) {
712  memcpy(&hdr_buf[hdr_len], intbuf, cur_len);
713  hdr_len += cur_len;
714  }
715  }
716 
717  if (keepalive) {
718  cur_string = g_psHTTPHeaderStrings[HTTP_HDR_CONN_KEEPALIVE];
719  } else {
720  cur_string = g_psHTTPHeaderStrings[HTTP_HDR_CONN_CLOSE];
721  }
722  cur_len = strlen(cur_string);
723  fprintf(data_file, NEWLINE "/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
724  written += file_put_ascii(data_file, cur_string, cur_len, &i);
725  i = 0;
726  if (precalcChksum) {
727  memcpy(&hdr_buf[hdr_len], cur_string, cur_len);
728  hdr_len += cur_len;
729  }
730  }
731 
732  cur_string = file_type;
733  cur_len = strlen(cur_string);
734  fprintf(data_file, NEWLINE "/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
735  written += file_put_ascii(data_file, cur_string, cur_len, &i);
736  i = 0;
737  if (precalcChksum) {
738  memcpy(&hdr_buf[hdr_len], cur_string, cur_len);
739  hdr_len += cur_len;
740 
741  LWIP_ASSERT("hdr_len <= 0xffff", hdr_len <= 0xffff);
742  LWIP_ASSERT("strlen(hdr_buf) == hdr_len", strlen(hdr_buf) == hdr_len);
743  acc = ~inet_chksum(hdr_buf, (u16_t)hdr_len);
744  *http_hdr_len = (u16_t)hdr_len;
745  *http_hdr_chksum = acc;
746  }
747 
748  return written;
749 }
750 
751 int file_put_ascii(FILE *file, const char* ascii_string, int len, int *i)
752 {
753  int x;
754  for(x = 0; x < len; x++) {
755  unsigned char cur = ascii_string[x];
756  fprintf(file, "0x%02.2x,", cur);
757  if ((++(*i) % HEX_BYTES_PER_LINE) == 0) {
758  fprintf(file, NEWLINE);
759  }
760  }
761  return len;
762 }
763 
764 int s_put_ascii(char *buf, const char *ascii_string, int len, int *i)
765 {
766  int x;
767  int idx = 0;
768  for(x = 0; x < len; x++) {
769  unsigned char cur = ascii_string[x];
770  sprintf(&buf[idx], "0x%02.2x,", cur);
771  idx += 5;
772  if ((++(*i) % HEX_BYTES_PER_LINE) == 0) {
773  sprintf(&buf[idx], NEWLINE);
774  idx += NEWLINE_LEN;
775  }
776  }
777  return len;
778 }
unsigned char precalcChksum
Definition: makefsdata.c:117
void concat_files(const char *file1, const char *file2, const char *targetfile)
Definition: makefsdata.c:311
#define FIND_T_IS_FILE(fInfo)
Definition: makefsdata.c:48
#define FINDFIRST_SUCCEEDED(ret)
Definition: makefsdata.c:53
struct file_entry * last_file
Definition: makefsdata.c:120
#define CHDIR_SUCCEEDED(ret)
Definition: makefsdata.c:58
uint32_t idx
Definition: lcd_log.c:247
unsigned char includeHttpHeader
Definition: makefsdata.c:114
#define NEWLINE
Definition: makefsdata.c:62
const char * serverID
Definition: makefsdata.c:74
int process_file(FILE *data_file, FILE *struct_file, const char *filename)
Definition: makefsdata.c:530
#define CHDIR(path)
Definition: makefsdata.c:57
unsigned char useHttp11
Definition: makefsdata.c:115
void process_file_data(const char *filename, FILE *data_file)
Definition: makefsdata.c:394
#define PAYLOAD_ALIGN_TYPE
Definition: makefsdata.c:81
u16_t inet_chksum(const void *dataptr, u16_t len)
Definition: inet_chksum.c:558
#define HEX_BYTES_PER_LINE
Definition: makefsdata.c:84
struct file_entry * next
Definition: makefsdata.c:92
#define NULL
Definition: usbd_def.h:53
Definition: makefsdata.c:90
#define GETCWD(path, len)
Definition: makefsdata.c:56
#define HTTPD_SERVER_AGENT
Definition: httpd_opts.h:85
int process_sub(FILE *data_file, FILE *struct_file)
Definition: makefsdata.c:324
#define MAX_PATH_LEN
Definition: makefsdata.c:86
struct file_entry * first_file
Definition: makefsdata.c:119
char lastFileVar[MAX_PATH_LEN]
Definition: makefsdata.c:110
#define NEWLINE_LEN
Definition: makefsdata.c:63
#define FINDNEXT_SUCCEEDED(ret)
Definition: makefsdata.c:54
int s_put_ascii(char *buf, const char *ascii_string, int len, int *i)
Definition: makefsdata.c:764
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
#define FIND_RET_T
Definition: makefsdata.c:49
#define PAYLOAD_ALIGNMENT
Definition: makefsdata.c:77
char curSubdir[MAX_PATH_LEN]
Definition: makefsdata.c:109
const char * filename_c
Definition: makefsdata.c:93
int write_checksums(FILE *struct_file, const char *filename, const char *varname, u16_t hdr_len, u16_t hdr_chksum)
Definition: makefsdata.c:423
int main(int argc, char *argv[])
Definition: makefsdata.c:136
int file_put_ascii(FILE *file, const char *ascii_string, int len, int *i)
Definition: makefsdata.c:751
#define FIND_T_IS_DIR(fInfo)
Definition: makefsdata.c:47
#define FINDFIRST_FILE(path, result)
Definition: makefsdata.c:50
unsigned char u8_t
Definition: cc.h:38
#define TCP_MSS
Definition: lwipopts.h:117
int file_write_http_header(FILE *data_file, const char *filename, int file_size, u16_t *http_hdr_len, u16_t *http_hdr_chksum)
Definition: makefsdata.c:596
#define FINDNEXT(ff_res, result)
Definition: makefsdata.c:52
#define COPY_BUFSIZE
Definition: makefsdata.c:88
#define FIND_T
Definition: makefsdata.c:45
#define FINDFIRST_DIR(path, result)
Definition: makefsdata.c:51
char hdr_buf[4096]
Definition: makefsdata.c:111
int check_path(char *path, size_t size)
Definition: makefsdata.c:271
unsigned char supportSsi
Definition: makefsdata.c:116
#define FIND_T_FILENAME(fInfo)
Definition: makefsdata.c:46
unsigned char processSubs
Definition: makefsdata.c:113
unsigned short u16_t
Definition: cc.h:40
int get_file_size(const char *filename)
Definition: makefsdata.c:379