int ra_nv_set(int argc,char **argv) { int index, rc; char *fz, *key, *value; if (argc == 1 || argc > 5) return set_usage(argv[0]); if (argc == 2) { fz = DEFAULT_FLASH_ZONE_NAME; key = argv[1]; value = ""; } else if (argc == 3) { fz = DEFAULT_FLASH_ZONE_NAME; key = argv[1]; value = argv[2]; } else if (argc == 4) { fz = argv[1]; key = argv[2]; value = argv[3]; } if ((index = getNvramIndex(fz)) == -1) { printf("%s: Error: \"%s\" flash zone not existed\n", argv[0], fz); return set_usage(argv[0]); } nvram_init(index); rc = nvram_set(index, key, value); nvram_close(index); return rc; }
int gamemap::read_header(const std::string& data) { // Test whether there is a header section size_t header_offset = data.find("\n\n"); if(header_offset == std::string::npos) { // For some reason Windows will fail to load a file with \r\n // lineending properly no problems on Linux with those files. // This workaround fixes the problem the copy later will copy // the second \r\n to the map, but that's no problem. header_offset = data.find("\r\n\r\n"); } const size_t comma_offset = data.find(","); // The header shouldn't contain commas, so if the comma is found // before the header, we hit a \n\n inside or after a map. // This is no header, so don't parse it as it would be. if (!(!(header_offset == std::string::npos || comma_offset < header_offset))) return 0; std::string header_str(std::string(data, 0, header_offset + 1)); config header; ::read(header, header_str); border_size_ = header["border_size"]; set_usage(header["usage"]); return header_offset + 2; }
void gamemap::read(const std::string& data, const bool allow_invalid, int border_size, std::string usage) { // Initial stuff border_size_ = border_size; set_usage(usage); tiles_.clear(); villages_.clear(); std::fill(startingPositions_, startingPositions_ + sizeof(startingPositions_) / sizeof(*startingPositions_), map_location()); std::map<int, t_translation::coordinate> starting_positions; if(data.empty()) { w_ = 0; h_ = 0; total_width_ = 0; total_height_ = 0; if(allow_invalid) return; } int offset = read_header(data); const std::string& data_only = std::string(data, offset); try { tiles_ = t_translation::read_game_map(data_only, starting_positions); } catch(t_translation::error& e) { // We re-throw the error but as map error. // Since all codepaths test for this, it's the least work. throw incorrect_map_format_error(e.message); } // Convert the starting positions to the array std::map<int, t_translation::coordinate>::const_iterator itor = starting_positions.begin(); for(; itor != starting_positions.end(); ++itor) { // Check for valid position, // the first valid position is 1, // so the offset 0 in the array is never used. if(itor->first < 1 || itor->first >= MAX_PLAYERS+1) { std::stringstream ss; ss << "Starting position " << itor->first << " out of range\n"; ERR_CF << ss.str(); ss << "The map cannot be loaded."; throw incorrect_map_format_error(ss.str().c_str()); } // Add to the starting position array startingPositions_[itor->first] = map_location(itor->second.x - 1, itor->second.y - 1); } // Post processing on the map total_width_ = tiles_.size(); total_height_ = total_width_ > 0 ? tiles_[0].size() : 0; w_ = total_width_ - 2 * border_size_; h_ = total_height_ - 2 * border_size_; //Disabled since there are callcases which pass along a valid map header but empty //map data. Still, loading (and actually applying) an empty map causes problems later on. //Other callcases which need to load a dummy map use completely empty data :(. //VALIDATE((w_ >= 1 && h_ >= 1), "A map needs at least 1 tile, the map cannot be loaded."); for(int x = 0; x < total_width_; ++x) { for(int y = 0; y < total_height_; ++y) { // Is the terrain valid? if(tcodeToTerrain_.count(tiles_[x][y]) == 0) { if(!try_merge_terrains(tiles_[x][y])) { std::stringstream ss; ss << "Illegal tile in map: (" << t_translation::write_terrain_code(tiles_[x][y]) << ") '" << tiles_[x][y] << "'\n"; ERR_CF << ss.str(); ss << "The map cannot be loaded."; throw incorrect_map_format_error(ss.str().c_str()); } } // Is it a village? if(x >= border_size_ && y >= border_size_ && x < total_width_-border_size_ && y < total_height_-border_size_ && is_village(tiles_[x][y])) { villages_.push_back(map_location(x-border_size_, y-border_size_)); } } } }
int main(int argc, char *argv[]) { int maxlen; uint64_t size; unsigned char *buffer; char mode; std::string s, line; mtx_common_init("base64tool", argv[0]); set_usage(); if (argc < 4) usage(0); mode = 0; if (!strcmp(argv[1], "encode")) mode = 'e'; else if (!strcmp(argv[1], "decode")) mode = 'd'; else mxerror(boost::format(Y("Invalid mode '%1%'.\n")) % argv[1]); maxlen = 72; if ((argc == 5) && (mode == 'e')) { if (!parse_number(argv[4], maxlen) || (maxlen < 4)) mxerror(Y("Max line length must be >= 4.\n\n")); } else if ((argc > 5) || ((argc > 4) && (mode == 'd'))) usage(2); maxlen = ((maxlen + 3) / 4) * 4; mm_io_cptr in, intext; try { in = mm_io_cptr(new mm_file_io_c(argv[2])); if (mode != 'e') intext = mm_io_cptr(new mm_text_io_c(in.get(), false)); } catch (mtx::mm_io::exception &ex) { mxerror(boost::format(Y("The file '%1%' could not be opened for reading: %2%.\n")) % argv[2] % ex); } mm_io_cptr out; try { out = mm_write_buffer_io_c::open(argv[3], 128 * 1024); } catch (mtx::mm_io::exception &ex) { mxerror(boost::format(Y("The file '%1%' could not be opened for writing: %2%.\n")) % argv[3] % ex); } in->save_pos(); in->setFilePointer(0, seek_end); size = in->getFilePointer(); in->restore_pos(); if (mode == 'e') { buffer = (unsigned char *)safemalloc(size); size = in->read(buffer, size); s = base64_encode(buffer, size, true, maxlen); safefree(buffer); out->write(s.c_str(), s.length()); } else { while (intext->getline2(line)) { strip(line); s += line; } buffer = (unsigned char *)safemalloc(s.length() / 4 * 3 + 100); try { size = base64_decode(s, buffer); } catch(...) { mxerror(Y("The Base64 encoded data could not be decoded.\n")); } out->write(buffer, size); safefree(buffer); } mxinfo(Y("Done.\n")); mxexit(0); }
inline void buffer<T>::configure( unsigned int components, unsigned int usage ) { set_components(components); set_usage(usage); m_configured = true; }
/* ** set - set resource values or print as notification, usage: "set [resource [value(s)]]", ** get help with "set help" */ int set (const char *argz) { char args[FNAMESIZE]; char *token=NULL, *subtoken=NULL, *subsubtoken=NULL; int slen=0, restidx=-1, sublen=-1, subsublen=-1; int x=0, y=0, ri=0, ret=0; memset(args, 0, sizeof(args)); strncpy(args, argz, sizeof(args)-2); /* reserve two zero bytes, we may overwrite one */ slen = parse_token(args, "\x09 ", &restidx); /* special case separately: empty line */ if (slen == 0) { if (cnf.bootup) set_usage(SHOW_CURRENT_VALUES); return (0); } args[slen] = '\0'; token = &args[0]; if (restidx > 0) { subtoken = &args[restidx]; sublen = parse_token(subtoken, "\x09 ", &restidx); subtoken[sublen] = '\0'; /* restore delimiter if required */ if (restidx > 0) { subsubtoken = &subtoken[restidx]; subsublen = strlen(subsubtoken); } } if (token[0] == '?') { if (cnf.bootup) set_usage(SHOW_USAGE); return (0); } else if (slen < 3 || token[0] == '#') { /* 1 or 2 char resource names and comments will be ignored */ return (0); } /* * test each setting one by one, (slen >= 3) now */ if (strncmp(token, "prefix", 6)==0) { SET_CHECK_B( GSTAT_PREFIX ); if (ret==0) { cnf.pref = (y==1) ? PREFIXSIZE : 0; } /* update! */ for (ri=0; ri<RINGSIZE; ri++) { if (cnf.fdata[ri].fflag & FSTAT_OPEN) update_curpos(ri); } //if (cnf.bootup) tracemsg ("prefix %d", (cnf.gstat & GSTAT_PREFIX) ? 1 : 0); } else if (strncmp(token, "tabhead", 7)==0) { SET_CHECK_B( GSTAT_TABHEAD ); if (ret==0) { cnf.head = (y==1) ? 2 : 1; } /* update! */ for (ri=0; ri<RINGSIZE; ri++) { if (cnf.fdata[ri].focus > TEXTROWS-1) cnf.fdata[ri].focus = TEXTROWS; } //if (cnf.bootup) tracemsg ("tabhead %d", (cnf.gstat & GSTAT_TABHEAD) ? 1 : 0); } else if (strncmp(token, "shadow", 6)==0) { SET_CHECK_B( GSTAT_SHADOW ); if (cnf.bootup) tracemsg ("shadow %d", (cnf.gstat & GSTAT_SHADOW) ? 1 : 0); } else if (strncmp(token, "smartindent", 5)==0) { SET_CHECK_B( GSTAT_SMARTIND ); if (cnf.bootup) tracemsg ("smartind %d", (cnf.gstat & GSTAT_SMARTIND) ? 1 : 0); } else if (strncmp(token, "move_reset", 10)==0) { SET_CHECK_B( GSTAT_MOVES ); if (cnf.bootup) tracemsg ("move_reset %d", (cnf.gstat & GSTAT_MOVES) ? 1 : 0); } else if (strncmp(token, "case_sensitive", 4)==0) { SET_CHECK_B( GSTAT_CASES ); if (cnf.bootup) tracemsg ("case_sensitive %d", (cnf.gstat & GSTAT_CASES) ? 1 : 0); } else if (strncmp(token, "autotitle", 9)==0) { SET_CHECK_B( GSTAT_AUTOTITLE ); if (cnf.bootup) tracemsg ("autotitle %d", (cnf.gstat & GSTAT_AUTOTITLE) ? 1 : 0); } else if (strncmp(token, "backup_nokeep", 13)==0) { SET_CHECK_B( GSTAT_NOKEEP ); if (cnf.bootup) tracemsg ("backup_nokeep %d", (cnf.gstat & GSTAT_NOKEEP) ? 1 : 0); } else if (strncmp(token, "close_over", 10)==0) { SET_CHECK_B( GSTAT_CLOS_OVER ); if (cnf.bootup) tracemsg ("close_over %d", (cnf.gstat & GSTAT_CLOS_OVER) ? 1 : 0); } else if (strncmp(token, "save_inode", 10)==0) { SET_CHECK_B( GSTAT_SAV_INODE ); if (cnf.bootup) tracemsg ("save_inode %d", (cnf.gstat & GSTAT_SAV_INODE) ? 1 : 0); } else if (strncmp(token, "tabsize", 4)==0) { /* decimal */ if (sublen > 0) { x = strtol(subtoken, NULL, 10); if (x < 2 || x > 16) { ret = 1; } else if (cnf.tabsize != x) { cnf.tabsize = x; if (cnf.bootup) { /* update! */ for (ri=0; ri<RINGSIZE; ri++) { if (cnf.fdata[ri].fflag & FSTAT_OPEN) update_curpos(ri); } } } } if (cnf.bootup) tracemsg ("tabsize %d", cnf.tabsize); } else if (strncmp(token, "indent", 6)==0) { if (sublen > 0) { ret = 1; x = 0; if (strncmp(subtoken, "tab", 3)==0) { cnf.gstat |= GSTAT_INDENT; x = 1; } else if (strncmp(subtoken, "space", 5)==0) { cnf.gstat &= ~GSTAT_INDENT; x = 2; } if (x) { if (subsublen > 0) { x = strtol(subsubtoken, NULL, 10); if (x > 7) { cnf.indentsize = 8; } else if (x < 2) { cnf.indentsize = 1; } else { cnf.indentsize = x; } ret = 0; } } } if (cnf.bootup) tracemsg ("indent %s %d", ((cnf.gstat & GSTAT_INDENT) ? "tab" : "space"), cnf.indentsize); } else if (strncmp(token, "tags_file", 4)==0) { SET_CHECK( cnf.tags_file ); } else if (strncmp(token, "find_opts", 9)==0) { if (sublen > 0) { subtoken[sublen] = ' '; /* overwrite zero, one multiword argument required */ sublen = strlen(subtoken); SET_CHECK( cnf.find_opts ); } else { if (cnf.bootup) tracemsg ("find_opts %s", cnf.find_opts); } } else if (!cnf.bootup && strncmp(token, "find_path", 9)==0) { SET_CHECK_X( cnf.find_path, sublen, subtoken ); } else if (strncmp(token, "make_opts", 9)==0) { if (sublen > 0) { subtoken[sublen] = ' '; /* overwrite zero, one multiword argument required */ sublen = strlen(subtoken); SET_CHECK( cnf.make_opts ); } else { if (cnf.bootup) tracemsg ("make_opts %s", cnf.make_opts); } } else if (!cnf.bootup && strncmp(token, "make_path", 9)==0) { SET_CHECK_X( cnf.make_path, sublen, subtoken ); } else if (!cnf.bootup && strncmp(token, "sh_path", 9)==0) { SET_CHECK_X( cnf.sh_path, sublen, subtoken ); } else if (!cnf.bootup && strncmp(token, "diff_path", 9)==0) { SET_CHECK_X( cnf.diff_path, sublen, subtoken ); /* vcs tool settings */ } else if (!cnf.bootup && strncmp(token, "vcstool", 7)==0) { if (sublen > 0) { ret = 1; for(x=0; x < 10; x++) { y = strlen(cnf.vcs_tool[x]); if (y==0 || !strncmp(subtoken, cnf.vcs_tool[x], (size_t)y)) { break; } } if (x < 10 && subsublen > 0) { y = sizeof(cnf.vcs_tool[x]); strncpy(cnf.vcs_tool[x], subtoken, (size_t)y); cnf.vcs_tool[x][y-1] = '\0'; ret = 0; SET_CHECK_X( cnf.vcs_path[x], subsublen, subsubtoken ); if (ret != 0) { cnf.vcs_tool[x][0] = '\0'; cnf.vcs_path[x][0] = '\0'; } } } } /* terminal color settings, color palette string parser -- shared prefix "palette" */ else if (!cnf.bootup && strncmp(token, "palette", 7)==0) { if (sublen > 0) { if ((subtoken[x] >= '0') && (subtoken[x] <= '9')) { x = strtol(subtoken, NULL, 0); if (x >= 0 && x < cnf.palette_count) cnf.palette = x; } else { /* pass input string to parser */ subtoken[sublen] = ' '; /* overwrite zero, one multiword argument required */ ret = color_palette_parser(subtoken); } } } /* lsdir sort settings */ else if (!strncmp(token, "sort", 4) || !strncmp(token, "lsdirsort", 9)) { if (sublen > 0) { x = strtol(subtoken, NULL, 0); if (x >= 0 && x <= 2) { if (x != cnf.lsdirsort) { cnf.lsdirsort = x; } } } if (cnf.bootup) tracemsg ("lsdirsort %d", cnf.lsdirsort); } /* syslog log levels by module */ else if (strncmp(token, "log", 3)==0) { int size2=0; char mystring[100]; size2 = sizeof(cnf.log) / sizeof(cnf.log[0]); if (sublen > 0) { subtoken[sublen] = ' '; /* overwrite zero, one multiword argument required */ sublen = strlen(subtoken); for(x=0; x < size2; x++) { cnf.log[x] = 0; } for(x=0,y=0; subtoken[x] != '\0' && y < size2; x++) { /* skip not-a-number */ if ((subtoken[x] >= '0') && (subtoken[x] <= '7')) { cnf.log[y] = (subtoken[x] - '0'); y++; } } } if (cnf.bootup && size2 > 11) { snprintf(mystring, sizeof(mystring), "MAIN%d FH%d CMD%d HIST%d REPL%d TAGS%d SELE%d FILT%d PIPE%d PD%d REC%d UPD%d", cnf.log[0],cnf.log[1],cnf.log[2],cnf.log[3],cnf.log[4],cnf.log[5], cnf.log[6],cnf.log[7],cnf.log[8],cnf.log[9],cnf.log[10],cnf.log[11]); tracemsg ("log %s", mystring); } } else { if (cnf.bootup) { set_usage(SHOW_USAGE); } else { fprintf(stderr, "eda: rc [%s] [%s] ... bad token\n", token, subtoken); ret = 2; } } return (ret); }/* set */
int main(int argc, char *argv[]) { int opt; long fin = 0; int use_pkix = 0; SECStatus rv; char pbuf[1024]; PRBool crlcheck = PR_FALSE; PRBool ocspcheck = PR_FALSE; PRBool strict = PR_FALSE; CERTCertDBHandle *handle = NULL; CERTCertificate **certout = NULL; CERTVerifyLog vfy_log; CERTVerifyLog vfy_log2; CERTVerifyLog *cur_log; CERTValOutParam *pkixout = NULL; SECItem c1; SECItem c2; SECItem *certs[2]; certs[0] = &c1; certs[1] = &c2; int numcerts = 0; while ((opt = getopt(argc, argv, "u:d:e:pn:s:coSr")) != -1) { switch(opt) { /* usage type */ case 'u': set_usage(optarg); break; case 'd': db_dir = optarg; break; case 's': sub_file = optarg; break; case 'c': crlcheck = PR_TRUE; break; case 'o': ocspcheck = PR_TRUE; break; case 'S': strict = PR_TRUE; break; case 'e': end_file = optarg; break; case 'p': use_pkix = 1; break; case 'n': rightca_nick = optarg; break; case 'r': retry_verify = PR_TRUE; break; default: print_usage(); break; } } if (db_dir == NULL) db_dir = "testfiles/"; if (end_file == NULL) end_file = "testfiles/end.pem"; get_file(certs[numcerts++], end_file); if (sub_file != NULL) { get_file(certs[numcerts++], sub_file); } snprintf(pbuf, sizeof(pbuf), "sql:%s", db_dir); if (NSS_Initialize(pbuf, "", "", "secmod.db", 0x1) != SECSuccess) { printf("NSS_Initialize failed %d\n", PORT_GetError()); exit(-1); } if ((handle = CERT_GetDefaultCertDB()) == NULL) { printf("NULL handle\n"); exit(-1); } if (ocspcheck) { CERT_EnableOCSPChecking(handle); CERT_DisableOCSPDefaultResponder(handle); if (strict) CERT_SetOCSPFailureMode(ocspMode_FailureIsNotAVerificationFailure); } rv = CERT_ImportCerts(handle, 0, numcerts, certs, &certout, PR_FALSE, PR_FALSE, NULL); if (rv != SECSuccess) { printf("CERT_ImportCerts failed %d\n", PORT_GetError()); exit(-1); } vfy_log.count = 0; vfy_log.head = NULL; vfy_log.tail = NULL; vfy_log.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); vfy_log2.count = 0; vfy_log2.head = NULL; vfy_log2.tail = NULL; vfy_log2.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); if (use_pkix) { int in_idx = 0; CERTValInParam cvin[7]; CERTValOutParam cvout[3]; CERTCertList *trustcl = NULL; CERTRevocationFlags rev; PRUint64 revFlagsLeaf[2] = { 0, 0 }; PRUint64 revFlagsChain[2] = { 0, 0 }; zero(&cvin); /* ??? is this reasonable? */ zero(&cvout); /* ??? is this reasonable? */ zero(&rev); /* ??? is this reasonable? */ if (rightca_nick == NULL) rightca_nick = "root"; if ((trustcl = get_trust_certlist(handle, rightca_nick)) == NULL) { printf("Couldn't find trust anchor\n"); exit(-1); } cvin[in_idx].type = cert_pi_useAIACertFetch; cvin[in_idx++].value.scalar.b = PR_TRUE; cvin[in_idx].type = cert_pi_revocationFlags; cvin[in_idx++].value.pointer.revocation = &rev; cvin[in_idx].type = cert_pi_trustAnchors; cvin[in_idx++].value.pointer.chain = trustcl; cvin[in_idx].type = cert_pi_useOnlyTrustAnchors; cvin[in_idx++].value.scalar.b = PR_TRUE; set_rev_per_meth(&rev, revFlagsLeaf, revFlagsChain); set_rev_params(&rev, crlcheck, ocspcheck, strict); cvin[in_idx].type = cert_pi_end; cvout[0].type = cert_po_errorLog; cvout[0].value.pointer.log = &vfy_log; cur_log = &vfy_log; cvout[1].type = cert_po_certList; cvout[1].value.pointer.chain = NULL; cvout[2].type = cert_po_end; pkixout = &cvout[0]; pkixredo: rv = CERT_PKIXVerifyCert(*certout, pkixusage, cvin, cvout, NULL); //CERT_DestroyCertList(trustcl); } else { cur_log = &vfy_log; vfyredo: rv = CERT_VerifyCert(handle, *certout, PR_TRUE, usage, PR_Now(), NULL, cur_log); } if (rv != SECSuccess || cur_log->count > 0) { if (cur_log->count > 0 && cur_log->head != NULL) { fin = err_stat(cur_log->head); } else { fin = PORT_GetError(); } if (fin == SEC_ERROR_INADEQUATE_KEY_USAGE) { printf("SEC_ERROR_INADEQUATE_KEY_USAGE : Certificate key usage inadequate for attempted operation.\n" ); } else if (fin == SEC_ERROR_INADEQUATE_CERT_TYPE) { printf("SEC_ERROR_INADEQUATE_CERT_TYPE : Certificate type not approved for application.\n" ); } else { printf("OTHER : %ld", fin); } } if ((fin == SEC_ERROR_INADEQUATE_CERT_TYPE || fin == SEC_ERROR_INADEQUATE_KEY_USAGE) && retry_verify && !retried) { printf("Retrying verification\n"); fin = 0; retried = PR_TRUE; if (use_pkix) { pkixout[0].value.pointer.log = &vfy_log2; cur_log = &vfy_log2; pkixout[1].value.pointer.chain = NULL; if (pkixusage == certificateUsageSSLClient) { pkixusage = certificateUsageSSLServer; } else { pkixusage = certificateUsageSSLClient; } goto pkixredo; } else { if (usage == certUsageSSLClient) { usage = certUsageSSLServer; } else { usage = certUsageSSLClient; } goto vfyredo; } } PORT_FreeArena(vfy_log.arena, PR_FALSE); PORT_FreeArena(vfy_log2.arena, PR_FALSE); NSS_Shutdown(); exit(fin == 0 ? 0 : 1); }