Changeset 8434
- Timestamp:
- 02/27/13 14:34:31 (11 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/module-webif-tpl.c
r8433 r8434 8 8 9 9 extern uint8_t cs_http_use_utf8; 10 extern const struct template templates[]; 11 12 static int8_t *tplchksum; 10 11 /* struct template templates[] that comes from webif/pages.c is recreated as 12 struct tpl tpls[] because we need to add additional fields such as tpl_name_hash 13 and possibly preprocess templates[] struct before using it. */ 14 15 struct tpl { 16 uint32_t tpl_name_hash; 17 const char *tpl_name; 18 const char *tpl_data; 19 const char *tpl_deps; 20 uint32_t tpl_data_len; 21 }; 22 23 static struct tpl *tpls; 24 static int tpls_count; 25 26 void webif_tpls_prepare(void) { 27 int i; 28 extern const struct template templates[]; 29 tpls_count = templates_count(); 30 if (!cs_malloc(&tpls, tpls_count * sizeof(struct tpl))) { 31 tpls_count = 0; 32 return; 33 } 34 for(i = 0; i < tpls_count; ++i) { 35 tpls[i].tpl_name_hash = jhash(templates[i].tpl_name, strlen(templates[i].tpl_name)); 36 tpls[i].tpl_name = templates[i].tpl_name; 37 tpls[i].tpl_data = templates[i].tpl_data; 38 tpls[i].tpl_deps = templates[i].tpl_deps; 39 tpls[i].tpl_data_len = templates[i].tpl_data_len; 40 } 41 } 42 43 void webif_tpls_free(void) { 44 free(tpls); 45 } 13 46 14 47 /* Adds a name->value-mapping or appends to it. You will get a reference back which you may freely … … 204 237 char *tpl_getUnparsedTpl(const char* name, int8_t removeHeader, const char* subdir) { 205 238 int32_t i; 206 int32_t tplcnt = tpl_count();207 239 char *result; 208 240 … … 316 348 } // if 317 349 318 int8_t chksum = 0; 319 for (i = strlen(name); i > 0; --i) { 320 chksum += name[i]; 321 } 322 323 for(i = 0; i < tplcnt; ++i) { 324 const struct template *tpl = &templates[i]; 325 if (tplchksum && chksum == tplchksum[i] && name[0] == tpl->tpl_name[0]) { // basic check to save strcmp calls as we are doing this hundreds of times per page in some cases 326 if (strcmp(name, tpl->tpl_name) == 0) break; 327 } 328 } 329 330 if (i >= 0 && i < tplcnt) { 331 const char *tpl_data = templates[i].tpl_data; 332 int32_t len = strlen(tpl_data) + 1; 333 if (!cs_malloc(&result, len)) return NULL; 334 memcpy(result, tpl_data, len); 350 bool found = 0; 351 uint32_t name_hash = jhash(name, strlen(name)); 352 for(i = 0; i < tpls_count; i++) { 353 if (tpls[i].tpl_name_hash == name_hash) { 354 found = 1; 355 break; 356 } 357 } 358 359 if (found) { 360 const struct tpl *tpl = &tpls[i]; 361 if (!cs_malloc(&result, tpl->tpl_data_len + 1)) return NULL; // +1 to accomodate \0 at the end 362 memcpy(result, tpl->tpl_data, tpl->tpl_data_len); 335 363 } else { 336 if (!cs_malloc(&result, 1)) return NULL; 337 result[0] = '\0'; 364 if (!cs_malloc(&result, 1)) return NULL; // Return empty string 338 365 } 339 366 return result; … … 397 424 /* Saves all templates to the specified paths. Existing files will be overwritten! */ 398 425 int32_t tpl_saveIncludedTpls(const char *path) { 399 int32_t tplcnt = tpl_count();400 426 int32_t i, cnt = 0; 401 427 char tmp[256]; 402 428 FILE *fp; 403 for (i = 0; i < tpl cnt; ++i) {404 const struct t emplate *tpl = &templates[i];429 for (i = 0; i < tpls_count; ++i) { 430 const struct tpl *tpl = &tpls[i]; 405 431 if (strlen(tpl_getTplPath(tpl->tpl_name, path, tmp, 256)) > 0 && (fp = fopen(tmp,"w")) != NULL) { 406 int32_t len = strlen(tpl->tpl_data);407 432 if (strncmp(tpl->tpl_name, "IC", 2) != 0) { 408 fprintf(fp, "<!--OSCam;%lu;%s;%s;%s-->\n", crc32(0L, (unsigned char *)tpl->tpl_data, len), CS_VERSION, CS_SVN_VERSION, tpl->tpl_deps);433 fprintf(fp, "<!--OSCam;%lu;%s;%s;%s-->\n", crc32(0L, (unsigned char *)tpl->tpl_data, tpl->tpl_data_len), CS_VERSION, CS_SVN_VERSION, tpl->tpl_deps); 409 434 } 410 fwrite(tpl->tpl_data, sizeof(char), len, fp);435 fwrite(tpl->tpl_data, tpl->tpl_data_len, 1, fp); 411 436 fclose (fp); 412 437 ++cnt; … … 420 445 char dirpath[255] = "\0"; 421 446 snprintf(dirpath, 255, "%s%s", cfg.http_tpl ? cfg.http_tpl : "", subdir); 422 int32_t i , tplcnt = tpl_count();447 int32_t i; 423 448 char path[255]; 424 for(i = 0; i < tpl cnt; ++i) {425 const struct t emplate *tpl = &templates[i];449 for(i = 0; i < tpls_count; ++i) { 450 const struct tpl *tpl = &tpls[i]; 426 451 if (strncmp(tpl->tpl_name, "IC", 2) != 0 && strlen(tpl_getTplPath(tpl->tpl_name, dirpath, path, 255)) > 0 && file_exists(path)) { 427 452 int8_t error = 1; 428 453 char *tplorg = tpl_getUnparsedTpl(tpl->tpl_name, 0, subdir); 429 unsigned long checksum = 0, curchecksum = crc32(0L, (unsigned char*)tpl->tpl_data, strlen(tpl->tpl_data));454 unsigned long checksum = 0, curchecksum = crc32(0L, (unsigned char*)tpl->tpl_data, tpl->tpl_data_len); 430 455 char *ifdefs = "", *pch1 = strstr(tplorg,"<!--OSCam"); 431 456 if (pch1 != NULL) { … … 487 512 } 488 513 489 /* Create some easy checksums (but they should be sufficient for our needs) in order to speedup lookup of templates. */490 void prepareTplChecksums(void) {491 int32_t i, j;492 int32_t tplcnt = tpl_count();493 if (!cs_malloc(&tplchksum, tplcnt))494 return;495 for(i = 0; i < tplcnt; ++i) {496 tplchksum[i] = 0;497 const char *tpl_name = templates[i].tpl_name;498 for(j = strlen(tpl_name); j > 0; --j) {499 tplchksum[i] += tpl_name[j];500 }501 }502 }503 504 514 /* Helper function for urldecode.*/ 505 515 static int32_t x2i(int32_t i) { -
trunk/module-webif-tpl.h
r8421 r8434 1 1 #ifndef MODULE_WEBIF_TPL_H_ 2 2 #define MODULE_WEBIF_TPL_H_ 3 4 #ifdef WEBIF 3 5 4 6 /* Templates: Adds a variable. The variable can be used as often as wanted. */ … … 26 28 }; 27 29 30 void webif_tpls_prepare(void); 31 void webif_tpls_free(void); 32 28 33 struct templatevars *tpl_create(void); 29 34 void tpl_clear(struct templatevars *vars); … … 45 50 void tpl_checkDiskRevisions(void); 46 51 47 void prepareTplChecksums(void);48 49 52 char *urlencode(struct templatevars *vars, char *str); 50 53 char *xml_encode(struct templatevars *vars, char *chartoencode); 51 54 char *sec2timeformat(struct templatevars *vars, int32_t seconds); 52 55 56 #else 57 static inline void webif_tpls_free(void) { return; } 53 58 #endif 59 60 #endif -
trunk/module-webif.c
r8432 r8434 5205 5205 /* Prepare base64 decoding array */ 5206 5206 b64prepare(); 5207 prepareTplChecksums();5207 webif_tpls_prepare(); 5208 5208 5209 5209 tpl_checkDiskRevisions(); -
trunk/oscam-string.c
r8421 r8434 429 429 } 430 430 431 // https://en.wikipedia.org/wiki/Jenkins_hash_function 432 uint32_t jhash(const char *key, size_t len) { 433 uint32_t hash, i; 434 for (hash = i = 0; i < len; i++) { 435 hash += key[i]; 436 hash += (hash << 10); 437 hash ^= (hash >> 6); 438 } 439 hash += (hash << 3); 440 hash ^= (hash >> 11); 441 hash += (hash << 15); 442 return hash; 443 } 444 431 445 /* Converts a char to it's hex representation. See char_to_hex on how to use it. */ 432 446 char to_hex(char code) -
trunk/oscam-string.h
r8421 r8434 36 36 37 37 unsigned long crc32(unsigned long crc, const unsigned char *buf, unsigned int len); 38 uint32_t jhash(const char *key, size_t len); 38 39 39 40 char to_hex(char code); -
trunk/oscam.c
r8395 r8434 17 17 #include "module-stat.h" 18 18 #include "module-webif.h" 19 #include "module-webif-tpl.h" 19 20 #include "module-cw-cycle-check.h" 20 21 #include "oscam-chk.h" … … 1473 1474 unlink(oscam_pidfile); 1474 1475 1476 webif_tpls_free(); 1475 1477 init_free_userdb(cfg.account); 1476 1478 cfg.account = NULL; -
trunk/webif/pages_gen.c
r8427 r8434 43 43 char file[128]; 44 44 char deps[256]; 45 uint32_t data_len; 45 46 enum { TXT, BIN } type; 46 47 } data[MAX_TEMPLATES]; … … 204 205 } 205 206 206 static void p arse_deps(int tpl_idx) {207 static void print_template(int tpl_idx) { 207 208 static bool ifdef_open = 0; 208 209 char *prev_deps = ""; … … 240 241 } 241 242 242 fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=%s%s, .tpl_deps=\"%s\" },\n",243 fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=%s%s, .tpl_deps=\"%s\", .tpl_data_len=%u },\n", 243 244 ident, 244 245 templates.data[tpl_idx].type == TXT ? "TPL" : "", ident, 245 deps); 246 deps, 247 templates.data[tpl_idx].data_len 248 ); 246 249 247 250 if (ifdef_open && strcmp(deps, next_deps) != 0) { … … 251 254 } 252 255 253 static voiddump_text(char *ident, uint8_t *buf, size_t buf_len) {256 static uint32_t dump_text(char *ident, uint8_t *buf, size_t buf_len) { 254 257 int i; 255 258 fprintf(output_file, "#define TPL%s \\\n\"", ident); … … 268 271 } 269 272 fprintf(output_file, "\"\n\n"); 273 return buf_len; 270 274 } 271 275 … … 282 286 } 283 287 284 static void dump_base64(char *ident, char *mime, uint8_t *buf, size_t buf_len) { 288 static uint32_t dump_base64(char *ident, char *mime, uint8_t *buf, size_t buf_len) { 289 char tpl_type[32]; 285 290 size_t b64_buf_len = buf_len * 4 + 16; 286 291 uint8_t *b64_buf = malloc(b64_buf_len); … … 288 293 die("%s: can't alloc %zd bytes\n", __func__, b64_buf_len); 289 294 int i, b64_len = b64_encode(buf, buf_len, b64_buf, b64_buf_len); 290 fprintf(output_file, "#define %s \"data:%s;base64,\\\n", ident, mime); 295 snprintf(tpl_type, sizeof(tpl_type), "data:%s;base64,", mime); 296 fprintf(output_file, "#define %s \"%s\\\n", ident, tpl_type); 291 297 for (i = 0; i < b64_len; i++) { 292 298 if (i && i % 76 == 0) … … 296 302 fprintf(output_file, "\"\n\n"); 297 303 free(b64_buf); 304 return strlen(tpl_type) + b64_len; 298 305 } 299 306 … … 311 318 fprintf(output_file, " char *tpl_data;\n"); 312 319 fprintf(output_file, " char *tpl_deps;\n"); 320 fprintf(output_file, " uint32_t tpl_data_len;\n"); 313 321 fprintf(output_file, "};\n"); 314 322 fprintf(output_file, "\n"); 315 fprintf(output_file, "int32_t t pl_count(void);\n");323 fprintf(output_file, "int32_t templates_count(void);\n"); 316 324 fprintf(output_file, "\n"); 317 325 fprintf(output_file, "#endif\n"); … … 328 336 for (i = 0; i < templates.num; i++) { 329 337 uint8_t *buf; 330 size_t buf_len ;338 size_t buf_len, data_len = 0; 331 339 readfile(templates.data[i].file, &buf, &buf_len); 332 340 switch (templates.data[i].type) { 333 case TXT: d ump_text(templates.data[i].ident, buf, buf_len); break;334 case BIN: d ump_base64(templates.data[i].ident, get_mime(templates.data[i].file), buf, buf_len); break;341 case TXT: data_len = dump_text(templates.data[i].ident, buf, buf_len); break; 342 case BIN: data_len = dump_base64(templates.data[i].ident, get_mime(templates.data[i].file), buf, buf_len); break; 335 343 } 336 344 free(buf); 345 templates.data[i].data_len = data_len; 337 346 } 338 347 339 348 fprintf(output_file, "const struct template templates[] = {\n"); 340 349 for (i = 0; i < templates.num; i++) { 341 p arse_deps(i);350 print_template(i); 342 351 } 343 352 fprintf(output_file, "};\n"); 344 353 fprintf(output_file, "\n"); 345 fprintf(output_file, "int32_t t pl_count(void) { return sizeof(templates) / sizeof(struct template); }\n");354 fprintf(output_file, "int32_t templates_count(void) { return sizeof(templates) / sizeof(struct template); }\n"); 346 355 fprintf(output_file, "\n"); 347 356 fprintf(output_file, "#endif\n");
Note:
See TracChangeset
for help on using the changeset viewer.