17 #define WIN32_LEAN_AND_MEAN 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) 39 #define GETCWD(path, len) GetCurrentDirectoryA(len, path) 40 #define CHDIR(path) SetCurrentDirectoryA(path) 41 #define CHDIR_SUCCEEDED(ret) (ret == TRUE) 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) 56 #define GETCWD(path, len) getcwd(path, len) 57 #define CHDIR(path) chdir(path) 58 #define CHDIR_SUCCEEDED(ret) (ret == 0) 62 #define NEWLINE "\r\n" 66 #define LWIP_HTTPD_DYNAMIC_HEADERS 1 67 #define LWIP_HTTPD_SSI 1 68 #include "../httpd_structs.h" 70 #include "../core/inet_chksum.c" 71 #include "../core/def.c" 77 #define PAYLOAD_ALIGNMENT 4 79 #define ALIGN_PAYLOAD 1 81 #define PAYLOAD_ALIGN_TYPE "unsigned int" 82 static int payload_alingment_dummy_counter = 0;
84 #define HEX_BYTES_PER_LINE 16 86 #define MAX_PATH_LEN 256 88 #define COPY_BUFSIZE 10240 96 int process_sub(FILE *data_file, FILE *struct_file);
97 int process_file(FILE *data_file, FILE *struct_file,
const char *filename);
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);
122 static void print_usage(
void)
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);
136 int main(
int argc,
char *argv[])
145 strcpy(targetfile,
"fsdata.c");
147 memset(path, 0,
sizeof(path));
148 memset(appPath, 0,
sizeof(appPath));
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);
155 for(i = 1; i < argc; i++) {
156 if (argv[i] ==
NULL) {
159 if (argv[i][0] ==
'-') {
160 if (strstr(argv[i],
"-s")) {
162 }
else if (strstr(argv[i],
"-e")) {
164 }
else if (strstr(argv[i],
"-11")) {
166 }
else if (strstr(argv[i],
"-nossi")) {
168 }
else if (strstr(argv[i],
"-c")) {
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"))) {
178 }
else if ((argv[i][0] ==
'/') && (argv[i][1] ==
'?') && (argv[i][2] == 0)) {
182 strncpy(path, argv[i],
sizeof(path)-1);
183 path[
sizeof(path)-1] = 0;
188 printf(
"Invalid path: \"%s\"." NEWLINE, path);
196 printf(
" Failed to open directory \"%s\"." NEWLINE NEWLINE, path);
202 printf(
"HTTP %sheader will %s statically included." NEWLINE,
207 printf(
" Processing all files in directory %s", path);
209 printf(
" and subdirectories..." NEWLINE NEWLINE);
211 printf(
"..." NEWLINE NEWLINE);
214 data_file = fopen(
"fsdata.tmp",
"wb");
215 if (data_file ==
NULL) {
216 printf(
"Failed to create file \"fsdata.tmp\"\n");
219 struct_file = fopen(
"fshdr.tmp",
"wb");
220 if (struct_file ==
NULL) {
221 printf(
"Failed to create file \"fshdr.tmp\"\n");
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);
232 fprintf(data_file,
"#define file_NULL (struct fsdata_file *) NULL" NEWLINE NEWLINE NEWLINE);
236 filesProcessed =
process_sub(data_file, struct_file);
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);
249 printf(NEWLINE
"Creating target file..." NEWLINE NEWLINE);
253 if (
remove(
"fsdata.tmp") != 0) {
254 printf(
"Warning: failed to delete fsdata.tmp\n");
256 if (
remove(
"fshdr.tmp") != 0) {
257 printf(
"Warning: failed to delete fshdr.tmp\n");
260 printf(NEWLINE
"Processed %d files - done." NEWLINE NEWLINE, filesProcessed);
262 while (first_file !=
NULL) {
264 first_file = fe->
next;
283 while ((slen > 0) && ((path[slen] ==
'\\') || (path[slen] ==
'/'))) {
294 static void copy_file(
const char *filename_in, FILE *fout)
298 fin = fopen(filename_in,
"rb");
300 printf(
"Failed to open file \"%s\"\n", filename_in);
304 while((len = fread(file_buffer_raw, 1,
COPY_BUFSIZE, fin)) > 0)
306 fwrite(file_buffer_raw, 1, len, fout);
311 void concat_files(
const char *file1,
const char *file2,
const char *targetfile)
314 fout = fopen(targetfile,
"wb");
316 printf(
"Failed to open file \"%s\"\n", targetfile);
319 copy_file(file1, fout);
320 copy_file(file2, fout);
328 int filesProcessed = 0;
333 size_t freelen =
sizeof(
curSubdir) - sublen - 1;
339 if ((curName[0] ==
'.') || (strcmp(curName,
"CVS") == 0)) {
348 strncat(
curSubdir, curName, freelen - 1);
351 filesProcessed +=
process_sub(data_file, struct_file);
355 printf(
"WARNING: cannot process sub due to path length restrictions: \"%s/%s\"\n",
curSubdir, curName);
368 if (
process_file(data_file, struct_file, curName) < 0) {
376 return filesProcessed;
383 inFile = fopen(filename,
"rb");
384 if (inFile ==
NULL) {
385 printf(
"Failed to open file \"%s\"\n", filename);
388 fseek(inFile, 0, SEEK_END);
389 file_size = ftell(inFile);
397 size_t len, written, i, src_off=0;
399 source_file = fopen(filename,
"rb");
400 if (source_file ==
NULL) {
401 printf(
"Failed to open file \"%s\"\n", filename);
407 len = fread(file_buffer_raw, 1,
COPY_BUFSIZE, source_file);
409 for (i = 0; i < len; i++) {
410 sprintf(&file_buffer_c[off],
"0x%02.2x,", file_buffer_raw[i]);
417 written = fwrite(file_buffer_c, 1, off, data_file);
431 #if LWIP_TCP_TIMESTAMPS 436 fprintf(struct_file,
"#if HTTPD_PRECALCULATED_CHECKSUM" NEWLINE);
437 fprintf(struct_file,
"const struct fsdata_chksum chksums_%s[] = {" NEWLINE, varname);
439 memset(file_buffer_raw, 0xab,
sizeof(file_buffer_raw));
440 f = fopen(filename,
"rb");
442 printf(
"Failed to open file \"%s\"\n", filename);
447 fprintf(struct_file,
"{%d, 0x%04x, %d}," NEWLINE, 0, hdr_chksum, hdr_len);
450 for (offset = hdr_len; ; offset += len) {
451 unsigned short chksum;
452 len = fread(file_buffer_raw, 1, chunk_size, f);
458 fprintf(struct_file,
"{%d, 0x%04x, %d}," NEWLINE, offset, chksum, len);
462 fprintf(struct_file,
"};" NEWLINE);
463 fprintf(struct_file,
"#endif /* HTTPD_PRECALCULATED_CHECKSUM */" NEWLINE);
467 static int is_valid_char_for_c_var(
char x)
469 if (((x >=
'A') && (x <=
'Z')) ||
470 ((x >=
'a') && (x <=
'z')) ||
471 ((x >=
'0') && (x <=
'9')) ||
479 static void fix_filename_for_c(
char* qualifiedName,
size_t max_len)
482 size_t len = strlen(qualifiedName);
483 char *new_name = malloc(len + 2);
487 if (len + 3 == max_len) {
488 printf(
"File name too long: \"%s\"\n", qualifiedName);
491 strcpy(new_name, qualifiedName);
492 for (i = 0; i < len; i++) {
493 if (!is_valid_char_for_c_var(new_name[i])) {
499 for (f = first_file; f !=
NULL; f = f->
next) {
504 sprintf(&new_name[len],
"%d", cnt);
508 }
while (!filename_ok && (cnt < 999));
510 printf(
"Failed to get unique file name: \"%s\"\n", qualifiedName);
513 strcpy(qualifiedName, new_name);
517 static void register_filename(
const char* qualifiedName)
522 if (first_file ==
NULL) {
523 first_file = last_file = fe;
525 last_file->
next = fe;
530 int process_file(FILE *data_file, FILE *struct_file,
const char *filename)
536 u16_t http_hdr_chksum = 0;
537 u16_t http_hdr_len = 0;
538 int chksum_count = 0;
541 sprintf(qualifiedName,
"%s/%s",
curSubdir, filename);
543 strcpy(varname, qualifiedName);
546 register_filename(varname);
549 fprintf(data_file,
"static const " PAYLOAD_ALIGN_TYPE " dummy_align_%s = %d;" NEWLINE, varname, payload_alingment_dummy_counter++);
551 fprintf(data_file,
"static const unsigned char data_%s[] = {" NEWLINE, varname);
553 fprintf(data_file,
"/* %s (%d chars) */" NEWLINE, qualifiedName, strlen(qualifiedName)+1);
554 file_put_ascii(data_file, qualifiedName, strlen(qualifiedName)+1, &i);
558 fprintf(data_file,
"0x%02.2x,", 0);
562 fprintf(data_file, NEWLINE);
569 chksum_count =
write_checksums(struct_file, filename, varname, http_hdr_len, http_hdr_chksum);
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);
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);
584 fprintf(struct_file,
"}};" NEWLINE NEWLINE);
589 fprintf(data_file, NEWLINE
"/* raw file data (%d bytes) */" NEWLINE, file_size);
591 fprintf(data_file,
"};" NEWLINE NEWLINE);
600 int response_type = HTTP_HDR_OK;
601 const char* file_type;
602 const char *cur_string;
607 const char *file_ext;
613 for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
614 if (strstr(filename, g_pcSSIExtensions[loop])) {
624 response_type = HTTP_HDR_OK_11;
627 fprintf(data_file,
NEWLINE "/* HTTP header */");
628 if (strstr(filename,
"404") == filename) {
629 response_type = HTTP_HDR_NOT_FOUND;
631 response_type = HTTP_HDR_NOT_FOUND_11;
633 }
else if (strstr(filename,
"400") == filename) {
634 response_type = HTTP_HDR_BAD_REQUEST;
636 response_type = HTTP_HDR_BAD_REQUEST_11;
638 }
else if (strstr(filename,
"501") == filename) {
639 response_type = HTTP_HDR_NOT_IMPL;
641 response_type = HTTP_HDR_NOT_IMPL_11;
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);
650 memcpy(&
hdr_buf[hdr_len], cur_string, cur_len);
655 cur_len = strlen(cur_string);
656 fprintf(data_file, NEWLINE
"/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
660 memcpy(&
hdr_buf[hdr_len], cur_string, cur_len);
665 if (file_ext !=
NULL) {
666 while(strstr(file_ext,
".") !=
NULL) {
667 file_ext = strstr(file_ext,
".");
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;
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;
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;
690 int content_len = file_size;
691 memset(intbuf, 0,
sizeof(intbuf));
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);
702 memcpy(&
hdr_buf[hdr_len], cur_string, cur_len);
706 _itoa(content_len, intbuf, 10);
707 strcat(intbuf,
"\r\n");
708 cur_len = strlen(intbuf);
712 memcpy(&
hdr_buf[hdr_len], intbuf, cur_len);
718 cur_string = g_psHTTPHeaderStrings[HTTP_HDR_CONN_KEEPALIVE];
720 cur_string = g_psHTTPHeaderStrings[HTTP_HDR_CONN_CLOSE];
722 cur_len = strlen(cur_string);
723 fprintf(data_file, NEWLINE
"/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
727 memcpy(&
hdr_buf[hdr_len], cur_string, cur_len);
732 cur_string = file_type;
733 cur_len = strlen(cur_string);
734 fprintf(data_file, NEWLINE
"/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len);
738 memcpy(&
hdr_buf[hdr_len], cur_string, cur_len);
741 LWIP_ASSERT(
"hdr_len <= 0xffff", hdr_len <= 0xffff);
744 *http_hdr_len = (
u16_t)hdr_len;
745 *http_hdr_chksum = acc;
754 for(x = 0; x < len; x++) {
755 unsigned char cur = ascii_string[x];
756 fprintf(file,
"0x%02.2x,", cur);
764 int s_put_ascii(
char *buf,
const char *ascii_string,
int len,
int *i)
768 for(x = 0; x < len; x++) {
769 unsigned char cur = ascii_string[x];
770 sprintf(&buf[idx],
"0x%02.2x,", cur);
unsigned char precalcChksum
void concat_files(const char *file1, const char *file2, const char *targetfile)
#define FIND_T_IS_FILE(fInfo)
#define FINDFIRST_SUCCEEDED(ret)
struct file_entry * last_file
#define CHDIR_SUCCEEDED(ret)
unsigned char includeHttpHeader
int process_file(FILE *data_file, FILE *struct_file, const char *filename)
void process_file_data(const char *filename, FILE *data_file)
#define PAYLOAD_ALIGN_TYPE
u16_t inet_chksum(const void *dataptr, u16_t len)
#define HEX_BYTES_PER_LINE
#define GETCWD(path, len)
#define HTTPD_SERVER_AGENT
int process_sub(FILE *data_file, FILE *struct_file)
struct file_entry * first_file
char lastFileVar[MAX_PATH_LEN]
#define FINDNEXT_SUCCEEDED(ret)
int s_put_ascii(char *buf, const char *ascii_string, int len, int *i)
#define LWIP_ASSERT(message, assertion)
#define PAYLOAD_ALIGNMENT
char curSubdir[MAX_PATH_LEN]
int write_checksums(FILE *struct_file, const char *filename, const char *varname, u16_t hdr_len, u16_t hdr_chksum)
int main(int argc, char *argv[])
int file_put_ascii(FILE *file, const char *ascii_string, int len, int *i)
#define FIND_T_IS_DIR(fInfo)
#define FINDFIRST_FILE(path, result)
int file_write_http_header(FILE *data_file, const char *filename, int file_size, u16_t *http_hdr_len, u16_t *http_hdr_chksum)
#define FINDNEXT(ff_res, result)
#define FINDFIRST_DIR(path, result)
int check_path(char *path, size_t size)
#define FIND_T_FILENAME(fInfo)
unsigned char processSubs
int get_file_size(const char *filename)