00001
00006 extern "C"
00007 {
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <unistd.h>
00011 }
00012
00013
00014
00015 bool SkipFermat = false;
00016 bool UsePhimat = false;
00017 bool SkipPhi = false;
00018 bool SkipFibonacci = false;
00019 bool SkipEasyECM = false;
00020 bool SkipECM = false;
00021 bool PrintSummary = false;
00022 int rho_Phase = 150000;
00023 int phi_Phase1 = 500000;
00024 double phi_Phase2 = 10000000.0;
00025 double elcu_Phase1 = 20000.0;
00026 double elcu_Phase2 = 2000000.0;
00027 int elcu_Kurven = 100;
00028 int ecm_curves_processed = 0;
00029
00030 double Factor_Threshold = 3.0;
00031
00032
00033
00034
00035
00036
00037
00038
00039 string XML_StatusFile;
00040
00041 string TempDir = "/tmp";
00042 string ConfigFile = "qsieve.cfg";
00043 string DynamicRelationsFile = "dynamic_relations.dat";
00044
00045 string ClientAccountName = "";
00046
00047 unsigned long int FFT_MAX_MEM_USAGE = 32*1024;
00048
00049
00050 #ifndef IS_CLIENT
00051 const string FactorizationsFile = "factorizations.txt";
00052 std::ofstream Factorization_to_file;
00053
00054
00055
00056
00057 #endif
00058
00059
00061 const string read_restrictedName(istream &is)
00062 {
00063 string s;
00064 while (is.good() && is.peek()!=EOF && isblank(is.peek())) is.get();
00065 while (is.good() && is.peek()!=EOF && s.length()<=40)
00066 {
00067 int i=is.get();
00068 if (isalnum(i) || i==' ' || i=='@' || i=='#' || i==',' || i=='.' || i=='+' || i=='-') s+=static_cast<char>(i);
00069 else return s;
00070 }
00071 return s;
00072 }
00073
00074 void Get_ConfigFile()
00075 {
00076
00077 if (getenv("QSIEVE_CFG"))
00078 {
00079 ConfigFile=getenv("QSIEVE_CFG");
00080 #ifdef VERBOSE_NOTICE
00081 cout << "Setting ConfigFile to \"" << ConfigFile << "\"" << endl;
00082 #endif
00083 }
00084
00085 #ifdef PATH_MAX
00086 char resolved_path[PATH_MAX];
00087 #else
00088 char resolved_path[4096];
00089 #endif
00090 struct stat buf;
00091
00092
00093 if (realpath(ConfigFile.c_str(),resolved_path) && (stat(resolved_path,&buf)==0))
00094 ConfigFile=resolved_path;
00095 else
00096 {
00097 cerr << "Cannot find ConfigFile " << "\"" << ConfigFile << "\"" << endl;
00098
00099 #if defined(etc_ConfigFile)
00100 cout << "trying to use \"" << etc_ConfigFile << "\"" << endl;
00101 if (realpath(etc_ConfigFile,resolved_path) && (stat(resolved_path,&buf)==0))
00102 {
00103 ConfigFile=resolved_path;
00104 return;
00105 }
00106 else
00107 cerr << "no ConfigFile found!" << endl;
00108 #endif
00109
00110
00111
00112 #ifndef IS_CLIENT
00113 exit(1);
00114 #endif
00115 }
00116 }
00117
00118
00119 void Read_ConfigFile(void)
00120 {
00121 Get_ConfigFile();
00122
00123 ifstream in(ConfigFile.c_str());
00124 char InputLine[200];
00125 string zeile;
00126 if (in)
00127 {
00128 #ifdef VERBOSE_INFO
00129 cout << "Reading configuration file" << endl;
00130 #ifdef IS_CLIENT
00131 cout << "Some server related options may have no effect to clients." << endl;
00132 #endif
00133 #endif
00134 while (!in.eof())
00135 {
00136 in.getline(InputLine,sizeof(InputLine),'\n');
00137 if (InputLine[0]=='#') continue;
00138 zeile=InputLine;
00139 while (zeile[0]==' ') zeile.erase(0,1);
00140 if (zeile.empty() || zeile[0]=='#') continue;
00141
00142
00143 istringstream is(zeile);
00144 string Keyword;
00145 is >> Keyword;
00146 if (Keyword=="DynamicRelationsFile")
00147 {
00148 string s;
00149 is >> s;
00150 if (s!="=")
00151 {
00152 cerr << "Error in " << ConfigFile << "!" << endl;
00153 exit(1);
00154 }
00155 is >> DynamicRelationsFile;
00156 continue;
00157 }
00158 if (Keyword=="WorkDir")
00159 {
00160 string s;
00161 is >> s;
00162 if (s!="=")
00163 {
00164 cerr << "Error in " << ConfigFile << "!" << endl;
00165 exit(1);
00166 }
00167 is >> s;
00168 #ifdef VERBOSE_NOTICE
00169 cout << "Setting current working directory to \"" << s << "\"." << endl;
00170 #endif
00171 if (chdir(s.c_str())!=0)
00172 {
00173 cerr << "Changing working directory failed!" << endl;
00174 exit(2);
00175 };
00176 continue;
00177 }
00178 if (Keyword=="TempDir")
00179 {
00180 string s;
00181 is >> s;
00182 if (s!="=")
00183 {
00184 cerr << "Error in " << ConfigFile << "!" << endl;
00185 exit(1);
00186 }
00187 is >> s;
00188 #ifdef VERBOSE_NOTICE
00189 cout << "Setting TempDir to \"" << s << "\"." << endl;
00190 #endif
00191 TempDir=s;
00192 continue;
00193 }
00194 if (Keyword=="XMLStatusFile")
00195 {
00196 string s;
00197 is >> s;
00198 if (s!="=")
00199 {
00200 cerr << "Error in " << ConfigFile << "!" << endl;
00201 exit(1);
00202 }
00203 is >> s;
00204 #ifdef VERBOSE_NOTICE
00205 cout << "Setting current XMLStatusFile to \"" << s << "\"." << endl;
00206 #endif
00207 XML_StatusFile=s;
00208 continue;
00209 }
00210 if (Keyword=="ClientAccount")
00211 {
00212 string s;
00213 is >> s;
00214 if (s!="=")
00215 {
00216 cerr << "Error in " << ConfigFile << "!" << endl;
00217 exit(1);
00218 }
00219 s=read_restrictedName(is);
00220 #ifdef VERBOSE_NOTICE
00221 cout << "Setting current ClientAccount to \"" << s << "\"." << endl;
00222 #endif
00223 ClientAccountName=s;
00224 continue;
00225 }
00226 if (Keyword=="SkipFermat")
00227 {
00228 SkipFermat=true;
00229 continue;
00230 }
00231 if (Keyword=="UsePhimat")
00232 {
00233 UsePhimat=true;
00234 continue;
00235 }
00236 if (Keyword=="SkipPhi")
00237 {
00238 SkipPhi=true;
00239 continue;
00240 }
00241 if (Keyword=="SkipFibonacci")
00242 {
00243 SkipFibonacci=true;
00244 continue;
00245 }
00246 if (Keyword=="SkipEasyECM")
00247 {
00248 SkipEasyECM=true;
00249 continue;
00250 }
00251 if (Keyword=="SkipECM")
00252 {
00253 SkipECM=true;
00254 continue;
00255 }
00256 if (Keyword=="PrintSummary")
00257 {
00258 PrintSummary=true;
00259 continue;
00260 }
00261 if (Keyword=="MemoryLimit")
00262 {
00263 string s;
00264 is >> s;
00265 if (s!="=")
00266 {
00267 cerr << "Error in " << ConfigFile << "!" << endl;
00268 exit(1);
00269 }
00270 is >> FFT_MAX_MEM_USAGE;
00271 FFT_MAX_MEM_USAGE<<=10;
00272 #ifdef VERBOSE_NOTICE
00273 cout << "Setting FFT MemoryLimit to " << (FFT_MAX_MEM_USAGE>>10) << "MB." << endl;
00274 #endif
00275 continue;
00276 }
00277 }
00278 }
00279 else
00280 {
00281 cout << ConfigFile << " not found! Using default values!" << endl;
00282 #ifndef IS_CLIENT
00283 static char ch = 'N';
00284 if (ch!='Y')
00285 {
00286 cout << "Please confirm with [Y]es." << endl;
00287 cin >> ch;
00288 if (ch!='Y') exit(1);
00289 }
00290 #endif
00291 }
00292
00293 #ifdef VERBOSE_INFO
00294 char cwd[4096];
00295 cout << "Current working directory: " << getcwd(cwd,4096) << endl;
00296 cout << "Configuration file: " << ConfigFile << endl;
00297 cout << "temp directory: " << TempDir << endl;
00298 cout << "DynamicRelationsFile: " << DynamicRelationsFile << endl;
00299 #endif
00300 #if !defined(IS_CLIENT) && !defined(IS_VALIDATOR)
00301
00302
00303 if (!Factorization_to_file.is_open()) Factorization_to_file.open(FactorizationsFile.c_str(),ios::out|ios::app);
00304 #endif
00305 }
00306
00307
00308 #ifndef IS_CLIENT
00309
00310 void tune_parameters(const unsigned int DecimalDigits)
00311 {
00312 ifstream in(ConfigFile.c_str());
00313 char InputLine[200];
00314 string zeile;
00315 unsigned int Stellen, gewaehlte_Stellenzahl=0;
00316 if (in)
00317 {
00318 #ifdef VERBOSE_INFO
00319 cout << "Reading configuration file" << endl;
00320 #endif
00321 while (!in.eof())
00322 {
00323 in.getline(InputLine,sizeof(InputLine),'\n');
00324 if (InputLine[0]=='#') continue;
00325 zeile=InputLine;
00326 while (zeile[0]==' ') zeile.erase(0,1);
00327 if (zeile.empty() || zeile[0]=='#') continue;
00328
00329
00330 istringstream is(zeile);
00331 string Keyword;
00332 is >> Keyword;
00333 if (Keyword=="SkipFermat") continue;
00334 if (Keyword=="UsePhimat") continue;
00335 if (Keyword=="SkipPhi") continue;
00336 if (Keyword=="SkipFibonacci") continue;
00337 if (Keyword=="SkipEasyECM") continue;
00338 if (Keyword=="SkipECM") continue;
00339 if (Keyword=="PrintSummary") continue;
00340 if (Keyword=="MemoryLimit") continue;
00341 if (Keyword=="DynamicRelationsFile" || Keyword=="WorkDir" || Keyword=="TempDir" || Keyword=="XMLStatusFile" || Keyword=="ClientAccount")
00342 {
00343 string s;
00344 is >> s;
00345 if (s!="=")
00346 {
00347 cerr << "Error in " << ConfigFile << "!" << endl;
00348 exit(1);
00349 }
00350
00351 continue;
00352 }
00353
00354
00355 Stellen = atoi(zeile.substr(0, zeile.find(":")).c_str());
00356
00357 if (Stellen==0)
00358 {
00359 cerr << "Error in " << ConfigFile << "!" << endl;
00360 cerr << "-> " << zeile << endl;
00361 exit(1);
00362 }
00363
00364 if (Stellen > DecimalDigits || Stellen==0) break;
00365 gewaehlte_Stellenzahl=Stellen;
00366 zeile.erase(0, zeile.find(":")+1);
00367 rho_Phase=atoi(zeile.substr(0, zeile.find(",")).c_str());
00368 zeile.erase(0, zeile.find(",")+1);
00369 phi_Phase1=atoi(zeile.substr(0, zeile.find(",")).c_str());
00370 zeile.erase(0, zeile.find(",")+1);
00371 phi_Phase2=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00372 zeile.erase(0, zeile.find(",")+1);
00373 elcu_Phase1=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00374 zeile.erase(0, zeile.find(",")+1);
00375 elcu_Phase2=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00376 zeile.erase(0, zeile.find(",")+1);
00377 elcu_Kurven=atoi(zeile.substr(0, zeile.find(",")).c_str());
00378 zeile.erase(0, zeile.find(",")+1);
00379 StaticFactorbase::Size_StaticFactorbase=atoi(zeile.substr(0, zeile.find(":")).c_str());
00380 zeile.erase(0, zeile.find(":")+1);
00381 Factor_Threshold = atof(zeile.substr(0, zeile.find(":")).c_str());
00382 if (Factor_Threshold<=0.01 || Factor_Threshold>10.0)
00383 {
00384 MARK;
00385 cerr << "invalid value of Factor_Threshold: " << Factor_Threshold << endl;
00386 exit(1);
00387 }
00388 zeile.erase(0, zeile.find(":")+1);
00389
00390 LogicalSieveSize = atoi(zeile.c_str());
00391 LogicalSieveSize = MAX(LogicalSieveSize,PhysicalSieveSize);
00392 }
00393 }
00394 else
00395 {
00396 cout << ConfigFile << " not found! Using default values!" << endl;
00397 static char ch = 'N';
00398 if (ch!='Y')
00399 {
00400 cout << "Please confirm with [Y]es." << endl;
00401 cin >> ch;
00402 if (ch!='Y') exit(1);
00403 }
00404 }
00405
00406 #ifdef VERBOSE_INFO
00407 cout << "Configuration:" << endl;
00408 cout << "#decimal digits: " << DecimalDigits << endl;
00409 cout << "actual parameters for " << gewaehlte_Stellenzahl << " digits" << endl;
00410 cout << "Pollard-Rho: " << rho_Phase << endl;
00411 cout << "Pollard-Phi: phase 1: " << phi_Phase1 << " phase 2: " << setprecision(1) << phi_Phase2 << endl;
00412 cout << "elliptic curves: phase 1: " << elcu_Phase1
00413 << " phase 2: " << elcu_Phase2
00414 << " #curves: " << elcu_Kurven << endl;
00415 cout << "Size of static factorbase: " << StaticFactorbase::Size() << endl;
00416 cout << "factor-threshold (exponent): " << Factor_Threshold << endl;
00417 cout << "sieve interval per polynomial: [" << -LogicalSieveSize << "," << LogicalSieveSize << "]" << endl;
00418 #endif
00419
00420 if (StaticFactorbase::Size()>StaticFactorbase::MaxSize)
00421 {
00422 cerr << "MaxSize_StaticFactorbase is too small (or Size_StaticFactorbase is to big)!" << endl;
00423 exit(1);
00424 }
00425 }
00426 #endif