// Save the Edited data to Disk void MatrixModel::saveDataCsv(bool close/*true*/) { //Check if there any existing changes if (changedData.isEmpty()) return; bool dataChanged = false; for (int r=0; r< _rows; r++) if (changedData[r].count(true)) dataChanged = true; if (!dataChanged) { this->notifyUser("No changes detected!!!"); return; } this->notifyUser("Saving your changes now\n Might take a minute!!!"); this->layoutAboutToBeChanged(); FILE* pFile = NULL; QString fileName = _fileName; fileName.insert(fileName.size()-4,"_cpy"); pFile = fopen(fileName.toStdString().c_str(), "w+"); if (pFile != NULL) { QByteArray dataToBeWritten; QList<QByteArray> rowData; for (int eachRow = 0; eachRow <_rows; eachRow++) { rowData = this->getRow(eachRow)[0]; //Cheack and take care of all edits if (!changedData.isEmpty() && changedData[eachRow].count(true)!=0) for (int c= 0; c < _columns; c++) if (changedData[eachRow][c]) rowData[c] = editedGridData[QPair<int,int>(eachRow,c)]; dataToBeWritten.append(rowData.join(',')); if (eachRow != _rows-1) dataToBeWritten += '\n'; changedData[eachRow].fill(false); fprintf(pFile,"%s", dataToBeWritten.constData()); dataToBeWritten.clear(); rowData.clear(); } fflush (pFile); fclose(pFile); if( remove( _fileName.toStdString().c_str() ) == 0 ) rename(fileName.toStdString().c_str(), _fileName.toStdString().c_str()); if (close) this->clear(); else //This means reading the complete files and resetting row reading.... // Time consuming for big Fies this->setRowBeginings(); this->layoutChanged(); } }
QString openGlContext() { QString result; QTextStream str(&result); QOpenGLContext context; if (context.create()) { # ifdef QT_OPENGL_DYNAMIC str << "Dynamic GL "; # endif switch (context.openGLModuleType()) { case QOpenGLContext::LibGL: str << "LibGL"; break; case QOpenGLContext::LibGLES: str << "LibGLES"; break; } QWindow window; if (QGuiApplication::platformName() == QLatin1String("greenisland")) window.setFlags(Qt::Desktop); window.setSurfaceType(QSurface::OpenGLSurface); //window.setScreen(QGuiApplication::primaryScreen()); window.create(); if (context.makeCurrent(&window)) { QOpenGLFunctions functions(&context); str << " Vendor: " << reinterpret_cast<const char *>(functions.glGetString(GL_VENDOR)) << "\nRenderer: " << reinterpret_cast<const char *>(functions.glGetString(GL_RENDERER)) << "\nVersion: " << reinterpret_cast<const char *>(functions.glGetString(GL_VERSION)) << "\nGLSL version: " << reinterpret_cast<const char *>(functions.glGetString(GL_SHADING_LANGUAGE_VERSION)) << "\nFormat: " << context.format(); QList<QByteArray> extensionList = context.extensions().toList(); std::sort(extensionList.begin(), extensionList.end()); QByteArray extensions = extensionList.join(' '); str << " \nFound " << extensionList.size() << " extensions:\n"; str << wordWrap(extensions, 78); context.doneCurrent(); } window.destroy(); } else { str << "Unable to create an Open GL context.\n"; } return result; }
QString AuthorizationManager::decryptString(QString str, QByteArray key){ bool pub=true; if(key.contains(" PUBLIC KEY--")){ pub=true; } else if(key.contains(" PRIVATE KEY--")){ pub=false; } else{ //unknown encryption - just return as-is if(!key.isEmpty()){ qDebug() << "Unknown key type!!" << key; } return str; } //Convert the input string into block elements as needed (and decode base64); QList<QByteArray> blocks; //QJsonDocument doc = QJsonDocument::fromJson(str.toLocal8Bit()); //if(doc.isNull()){ //No individual blocks - just one string QByteArray bytes; bytes.append(str); blocks << QByteArray::fromBase64(bytes); /*}else if(doc.isArray()){ for(int i=0; i<doc.array().count(); i++){ QByteArray bytes; bytes.append(doc.array()[i].toString()); blocks << QByteArray::fromBase64(bytes); } }else{ //Already valid JSON - return it return str; }*/ //qDebug() << "Decoded String:" << bytes; return QString(blocks.join()); //TEMPORARY BYPASS //qDebug() << "Start decoding String:" << pub << str;//<< key; //Reset/Load some SSL stuff //OpenSSL_add_all_algorithms(); //ERR_load_crypto_strings(); QString outstring; //output string //unsigned char *decode = (unsigned char*)malloc(5*bytes.size()); RSA *rsa= NULL; BIO *keybio = NULL; //qDebug() << " - Generate keybio"; keybio = BIO_new_mem_buf(key.data(), -1); if(keybio==NULL){ return ""; } //qDebug() << " - Read pubkey"; if(pub){ //PUBLIC KEY rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL); if(rsa==NULL){ qDebug() << " - Invalid Public RSA key!!" << key; BIO_free_all(keybio); return ""; } //decode = (unsigned char*)malloc( RSA_size(rsa) ); //qDebug() << " - Decrypt string"; for(int i=0; i<blocks.length(); i++){ unsigned char *decode = (unsigned char*)malloc(2*blocks[i].size()); int len = RSA_public_decrypt(blocks[i].size(), (unsigned char*)(blocks[i].data()), decode, rsa, RSA_PKCS1_PADDING); if(len<0){ qDebug() << " - Could not decrypt"; qDebug() << ERR_error_string (ERR_peek_error(), NULL); qDebug() << ERR_error_string (ERR_peek_last_error(), NULL); outstring.clear(); break; } //qDebug() << " - done"; outstring.append( QString( QByteArray( (char*)(decode), len) ) ); } RSA_free(rsa); BIO_free_all(keybio); }else{ //PRIVATE KEY rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL); if(rsa==NULL){ qDebug() << " - Invalid RSA key!!"; BIO_free_all(keybio); return ""; } //decode = (unsigned char*)malloc( RSA_size(rsa) ); //qDebug() << " - Decrypt string"; for(int i=0; i<blocks.length(); i++){ unsigned char *decode = (unsigned char*)malloc(2*blocks[i].size()); int len = RSA_private_decrypt(blocks[i].size(), (unsigned char*)(blocks[i].data()), decode, rsa, RSA_PKCS1_PADDING); if(len<0){ qDebug() << " - Could not decrypt"; qDebug() << ERR_error_string (ERR_peek_error(), NULL); qDebug() << ERR_error_string (ERR_peek_last_error(), NULL); outstring.clear(); break; } //qDebug() << " - done"; outstring.append( QString( QByteArray( (char*)(decode), len) ) ); } RSA_free(rsa); BIO_free_all(keybio); } return outstring; }
c_exp c_exp::fromLispExp(lisp_exp exp, QHash<QString, DataType> dataTypes, QHash<QString, QString> wireNames) { if (exp.isLeaf()) { if (wireNames.contains(exp.value())) { DataType dt = dataTypes.value(exp.value()); QString wireName = wireNames.value(exp.value()); return c_exp(wireName, dt); } else { bool success; exp.value().toInt(&success); if (success) { return c_exp(exp.value(), DATATYPE_INT); } else { exp.value().toFloat(&success); if (success) { return c_exp(exp.value(), DATATYPE_FLOAT); } else { return c_exp(); // error condition } } } } else { QString theOperator = exp.element(0).value(); if (theOperator == "+" || theOperator == "-") { QList<c_exp> results = evaluateArguments(exp, dataTypes, wireNames); DataType mostGeneralType = getMostGeneralType(results); DataType resultType; if (mostGeneralType.isAFP()) { resultType = DATATYPE_AFP(mostGeneralType.afpPrecision() - intLog2(results.size())); } else { resultType = mostGeneralType; } QList<QString> conversionCodes = getConversionCodes(results, resultType); QString code = conversionCodes.join(" " + theOperator + " "); return c_exp(code, resultType); } else if (theOperator == "*") { QList<c_exp > results = evaluateArguments(exp, dataTypes, wireNames); DataType mostGeneralType = getMostGeneralType(results); if (mostGeneralType.isAFP()) { QString lastCode = "(int64_t)(" + results[0].code() + ")"; DataType lastType = results[0].type(); for (int i = 1; i < results.size(); i++) { c_exp result = results[i]; if (! result.isValid()) { lastType = DataType(); } lastCode = "(" + lastCode + ") * (" + result.code() + ")"; lastCode = "(" + lastCode + ") >> 32"; int lastAFPPrecision = lastType.isAFP() ? lastType.afpPrecision() : 0; int resultAFPPrecision = result.type().isAFP() ? result.type().afpPrecision() : 0; if (lastType.isValid()) { lastType = DATATYPE_AFP(32 - ((32 - lastAFPPrecision) + (32 - resultAFPPrecision))); } } lastCode = "(int)(" + lastCode + ")"; return c_exp(lastCode, lastType); } else { DataType resultType = mostGeneralType; QList<QString> codes = getCodes(results); QString code = codes.join(" * "); return c_exp(code, resultType); } } else if (theOperator == "/") { return c_exp(); } else if (theOperator == "//") { return c_exp(); } else if (theOperator == "%") { return c_exp(); } else if (theOperator == "mod") { return c_exp(); } else if (theOperator == "if") { c_exp condResult = c_exp::fromLispExp(exp.element(1), dataTypes, wireNames); c_exp ifTrueResult = c_exp::fromLispExp(exp.element(2), dataTypes, wireNames); c_exp ifFalseResult = c_exp::fromLispExp(exp.element(3), dataTypes, wireNames); DataType resultType = moreGeneralType(ifTrueResult.type(), ifFalseResult.type()); QString code = "(" + condResult.code() + ") ? (" + ifTrueResult.conversionTo(resultType).code() + ") : (" + ifFalseResult.conversionTo(resultType).code() + ")"; return c_exp(code, resultType); } else if (theOperator == ">" || theOperator == "<" || theOperator == ">=" || theOperator == "<=" || theOperator == "==" || theOperator == "!=") { c_exp leftOperand = c_exp::fromLispExp(exp.element(1), dataTypes, wireNames); c_exp rightOperand = c_exp::fromLispExp(exp.element(2), dataTypes, wireNames); DataType resultType = moreGeneralType(leftOperand.type(), rightOperand.type()); QString code = "(" + leftOperand.conversionTo(resultType).code() + ") " + theOperator + " (" + rightOperand.conversionTo(resultType).code() + ")"; return c_exp(code, resultType); } else if (theOperator == "sqrt") { c_exp operand = c_exp::fromLispExp(exp.element(1), dataTypes, wireNames); QString code = "sqrt(" + operand.code() + ")"; return c_exp(code, DATATYPE_FLOAT); } else if (theOperator == "read_adc") { QString code = "((int)read_adc(" + c_exp::fromLispExp(exp.element(1), dataTypes, wireNames).conversionTo(DATATYPE_INT).code() + ") << 16)"; return c_exp(code, DATATYPE_AFP(27)); } else { return c_exp(); // error condition } } }