// restore nvram from file int nvram_restore(char *file, char *buf) { FILE *fp; char header[8], *p, *v; unsigned long count, *filelen; if ((fp=fopen(file, "r+"))==NULL) return -1; count = fread(header, 1, 8, fp); if (count>=8 && strncmp(header, PROFILE_HEADER, 4)==0) { filelen = (unsigned long *)(header + 4); count = fread(buf, 1, *filelen, fp); } fclose(fp); p = buf; while (*p) { //printf("nv:%s\n", p); v = strchr(p, '='); if (v!=NULL) { *v++ = '\0' /*eric--NULL*/; if (issyspara(p)) { nvram_set(p, v); } p = v + strlen(v) + 1; } else { nvram_unset(p); p = p + 1; } } nvram_set("x_Setting", "1"); return 0; }
int nvram_restore_new(char *file, char *buf) { FILE *fp; char header[8], *p, *v; unsigned long count, filelen, *filelenptr, i; unsigned char rand, *randptr; if ((fp = fopen(file, "r+")) == NULL) return -1; count = fread(header, 1, 8, fp); if (count>=8 && strncmp(header, PROFILE_HEADER, 4) == 0) { filelenptr = (unsigned long *)(header + 4); #ifdef ASUS_DEBUG fprintf(stderr, "restoring original text cfg of length %x\n", *filelenptr); #endif fread(buf, 1, *filelenptr, fp); } else if (count>=8 && strncmp(header, PROFILE_HEADER_NEW, 4) == 0) { filelenptr = (unsigned long *)(header + 4); filelen = *filelenptr & 0xffffff; #ifdef ASUS_DEBUG fprintf(stderr, "restoring non-text cfg of length %x\n", filelen); #endif randptr = (unsigned char *)(header + 7); rand = *randptr; #ifdef ASUS_DEBUG fprintf(stderr, "non-text cfg random number %x\n", rand); #endif count = fread(buf, 1, filelen, fp); #ifdef ASUS_DEBUG fprintf(stderr, "non-text cfg count %x\n", count); #endif for (i = 0; i < count; i++) { if ((unsigned char) buf[i] > ( 0xfd - 0x1)){ /* e.g.: to skip the case: 0x61 0x62 0x63 0x00 0x00 0x61 0x62 0x63 */ if(i > 0 && buf[i-1] != 0x0) buf[i] = 0x0; } else buf[i] = 0xff + rand - buf[i]; } #ifdef ASUS_DEBUG for (i = 0; i < count; i++) { if (i % 16 == 0) fprintf(stderr, "\n"); fprintf(stderr, "%2x ", (unsigned char) buf[i]); } for (i = 0; i < count; i++) { if (i % 16 == 0) fprintf(stderr, "\n"); fprintf(stderr, "%c", buf[i]); } #endif } else { fclose(fp); return 0; } fclose(fp); p = buf; while (*p) { #if 1 /* e.g.: to skip the case: 00 2e 30 2e 32 38 00 ff 77 61 6e */ if(*p == NULL || *p < 32 || *p > 127 ){ p = p + 1; continue; } #endif v = strchr(p, '='); if (v != NULL) { *v++ = '\0'; if (issyspara(p)) nvram_set(p, v); p = v + strlen(v) + 1; } else { nvram_unset(p); p = p + 1; } } return 0; }