void deliveryController::lg(QueryString &qs) { Log &error = app->GetErrorLog(); int zoneid = 0, bannerId = 0, campaignid = 0; try { zoneid = boost::lexical_cast<int>(qs["zoneid"]); bannerId = boost::lexical_cast<int>(qs["bannerid"]); if (!qs["campaignid"].empty()) campaignid = boost::lexical_cast<int>(qs["campaignid"]); } catch (boost::bad_lexical_cast) { error.LogFmt("lg action cast error: zoneid [%s], bannerid [%s], campaignid [%s]", qs["zoneid"].c_str(), qs["bannerid"].c_str(), qs["campaignid"].c_str()); response->DataRefResult(DataRef(gif_buf, 43)); return ; } if (!qs["campaignid"].empty()) { std::string impc = "impc[" + qs["campaignid"] + "]"; response->setCookie(impc, "1", DateTime::YearFromNow(1), "/"); } std::string impb = "impb[" + qs["bannerid"] + "]"; response->setCookie(impb, "1", DateTime::YearFromNow(1), "/"); response->DataRefResult(DataRef(gif_buf, 43)); return ; }
const uint8_t* ArchiveReader::FileLength( const uint8_t* start, const uint8_t* end ) { while ( start != end && m_pos != m_length ) m_buffer[m_pos++] = *start++; if ( m_pos == m_length ) { m_fileLength = ReadLittleEndian( DataRef( m_buffer, m_buffer + m_length ) ); std::string utf8Path = m_pathIsUTF8 ? reinterpret_cast<const char*>( m_path ) : LocalPathToUTF8Path( reinterpret_cast<const char*>( m_path ) ); if ( m_isDirectory ) { if ( m_fileLength ) throw Error( "A directory has a non-zero file length." ); m_output.Directory( utf8Path.c_str(), m_mtime ); m_state = STATE_PATH_ENCODING; } else { m_fileStream = &m_output.File( utf8Path.c_str(), m_mtime, m_fileLength ); m_filePos = 0; m_state = STATE_FILE; } } return start; }
const uint8_t* ArchiveReader::Path( const uint8_t* start, const uint8_t* end ) { while ( start != end && m_pos != m_length ) { uint8_t c = *start++; if ( c == '\\' ) c = '/'; m_path[m_pos++] = c; } if ( m_pos == m_length ) { if ( m_path[m_pos - 1] == 0 ) m_length--; else m_path[m_pos] = 0; if ( HasDotDotComponent( DataRef( m_path, m_path + m_length ) ) ) throw Error( "Paths in archives cannot contain '..'" ); m_isDirectory = ( m_length > 0 ) && ( m_path[m_length - 1] == '/' ); m_state = STATE_DATE_LENGTH; } return start; }
static bool HasDotDotComponent( DataRef path ) { const uint8_t* start = path.Start(); const uint8_t* end = path.End(); while ( true ) { const uint8_t* slash = std::find( start, end, '/' ); if ( DataRef( start, slash ) == DataRef( ".." ) ) return true; if ( slash == end ) return false; start = slash + 1; } }
OperationContext::DataRef OperationContext::getData(const String& key) const { SortedVectorMap<String, DataRef>::const_iterator ci = m_data.find(key); if (ci != m_data.end()) { return ci->second; } return DataRef(); }
std::size_t DataRefInStream::read(OutStream& os, std::size_t len) { auto size(std::min(mData.size() - mPosition, len)); os.write(DataRef(mData.begin() + mPosition, mData.begin() + mPosition + size)); mPosition += size; return size; }
DateTime::DateTime( DataRef date ) { // Example: Sun, 11 Mar 1984 00:00:00 +0000 Verify( date.Length() == 31 ); m_dayOfWeek = ReadDayOfWeek( date.Slice( 0, 3 ) ); Verify( date.Slice( 3, 2 ) == DataRef( ", " ) ); m_day = ReadAsciiNumber<uint8_t>( date.Slice( 5, 2 ) ); Verify( date[7] == ' ' ); m_month = ReadMonth( date.Slice( 8, 3 ) ); Verify( date[11] == ' ' ); m_year = ReadAsciiNumber<uint32_t>( date.Slice( 12, 4 ) ); Verify( date[16] == ' ' ); m_hour = ReadAsciiNumber<uint8_t>( date.Slice( 17, 2 ) ); Verify( date[19] == ':' ); m_minute = ReadAsciiNumber<uint8_t>( date.Slice( 20, 2 ) ); Verify( date[22] == ':' ); m_second = ReadAsciiNumber<uint8_t>( date.Slice( 23, 2 ) ); Verify( date[25] == ' ' ); Verify( date.Slice( 25, 6 ) == DataRef( " +0000" ) ); }
const uint8_t* ArchiveReader::Date( const uint8_t* start, const uint8_t* end ) { while ( start != end && m_pos != m_length ) m_buffer[m_pos++] = *start++; if ( m_pos == m_length ) { m_mtime = DateTime( DataRef( m_buffer, m_buffer + m_length ) ); m_length = m_fileLengthLength; m_pos = 0; m_state = STATE_FILE_LENGTH; } return start; }
TEST(CryptoInStream, Simple) { const uint8_t AesFile[] = { 0x41, 0x45, 0x53, 0x02, 0x00, 0x00, 0x19, 0x43, 0x52, 0x45, 0x41, 0x54, 0x45, 0x44, 0x5f, 0x42, 0x59, 0x00, 0x70, 0x79, 0x41, 0x65, 0x73, 0x43, 0x72, 0x79, 0x70, 0x74, 0x20, 0x30, 0x2e, 0x33, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb3, 0x1f, 0x6c, 0xe9, 0x00, 0xa9, 0x36, 0x9f, 0x12, 0xf5, 0x75, 0x0e, 0x37, 0xef, 0x17, 0xaa, 0x5a, 0x29, 0xfe, 0xb5, 0x7a, 0x1b, 0x92, 0xea, 0x08, 0xae, 0x92, 0x35, 0xa2, 0xc7, 0x8d, 0x55, 0x13, 0x6a, 0x5d, 0x7f, 0xcc, 0x28, 0x74, 0x3f, 0x8c, 0x05, 0x0e, 0x45, 0xd4, 0xc7, 0x94, 0x29, 0x65, 0xc7, 0x29, 0xc7, 0xc5, 0x20, 0xef, 0x51, 0xe7, 0x64, 0x57, 0x59, 0x23, 0x21, 0xc6, 0x7c, 0xdf, 0x2a, 0xb7, 0x08, 0xf5, 0x1e, 0xef, 0xa9, 0x49, 0x55, 0xc7, 0xff, 0x4d, 0xb0, 0x98, 0x0d, 0x53, 0xf8, 0x69, 0xd6, 0x71, 0x07, 0x25, 0xe7, 0xd2, 0xec, 0x02, 0x4e, 0x80, 0x40, 0xd8, 0x86, 0x4d, 0xf4, 0xf7, 0xfc, 0xde, 0xc0, 0x15, 0xac, 0xea, 0x56, 0xfc, 0x82, 0x1f, 0x85, 0x3e, 0x32, 0x0f, 0x0b, 0x44, 0xd4, 0x35, 0xa7, 0x87, 0xd3, 0x7b, 0xda, 0x06, 0xda, 0x5b, 0xa9, 0x3e, 0x9c, 0xd1, 0x9c, 0xfc, 0x35, 0x3d, 0xbb, 0x89, 0xaa, 0xa7, 0xbc, 0x0d, 0x3a, 0x41, 0x75, 0x43, 0xbd, 0xfd }; DataRefInStream is(DataRef(AesFile, AesFile + sizeof(AesFile))); CryptoInStream cis(is, "test"); // FIXME: Incorporate password suffix. // ASSERT_EQ(cis.error(), CryptoInStream::Error::kNone); std::array<uint8_t, 100> output; output.fill(0); ArrayOutStream out(output); DataRef expected("This is a test\n"); // ASSERT_EQ(cis.read(out, out.remaining()), expected.size()); // ASSERT_EQ(out.data(), expected); }
const uint8_t* ArchiveReader::File( const uint8_t* start, const uint8_t* end ) { uint64_t amount = std::min<uint64_t>( end - start, m_fileLength - m_filePos ); if ( amount ) { m_fileStream->Write( DataRef( start, start + static_cast<size_t>( amount ) ) ); start += amount; m_filePos += amount; } if ( m_filePos == m_fileLength ) { m_fileStream->End(); m_fileStream = 0; m_state = STATE_PATH_ENCODING; } return start; }
void renderLabel(RenderContext& rc, Label::Chunk *fi) { // FIXME: RTL support int oldSize = rc.size; int oldY = rc.y; int oldX = rc.x; while (fi) { if (fi->vOffset != Label::Chunk::None) { if (fi->vOffset == Label::Chunk::Up) { rc.y -= int(0.4 * rc.fontHeight()); } else { // Down rc.y += int(0.4 * rc.fontHeight()); } if (rc.size > 5) { rc.size = (rc.size*2)/3; } } QFont f = rc.font(); if (rc.fontSize() != rc.size) { f.setPointSize(rc.size); } f.setBold(fi->attributes.bold); f.setItalic(fi->attributes.italic); f.setUnderline(fi->attributes.underline); if (rc.p && fi->attributes.color.isValid()) { rc.p->setPen(fi->attributes.color); } else if (rc.p) { rc.p->setPen(rc.pen); } rc.setFont(f); if (fi->linebreak) { rc.x = oldX; rc.y += rc.fontAscent() + rc.fontDescent() + 1; fi = fi->next; continue; } if (!rc.substitute && (fi->scalar || fi->vector)) { QString txt = QString("[") + fi->text + "]"; if (rc.p) { rc.p->drawText(rc.x, rc.y, txt); } rc.x += rc.fontWidth(txt); } else if (fi->scalar) { // do scalar/string/fit substitution QString txt; if (!fi->text.isEmpty() && fi->text[0] == '=') { // Parse and evaluate as an equation bool ok = false; const double eqResult(Equation::interpret(fi->text.mid(1).latin1(), &ok)); txt = QString::number(eqResult, 'g', rc.precision); if (rc._cache) { rc._cache->append(DataRef(DataRef::DRExpression, fi->text, QString::null, 0.0, QVariant(eqResult))); } } else { KST::scalarList.lock().readLock(); KstScalarPtr scp = *KST::scalarList.findTag(fi->text); KST::scalarList.lock().unlock(); if (scp) { scp->readLock(); txt = QString::number(scp->value(), 'g', rc.precision); if (rc._cache) { rc._cache->append(DataRef(DataRef::DRScalar, fi->text, QString::null, 0.0, QVariant(scp->value()))); } scp->unlock(); } else { KST::stringList.lock().readLock(); KstStringPtr stp = *KST::stringList.findTag(fi->text); KST::stringList.lock().unlock(); if (stp) { stp->readLock(); txt = stp->value(); if (rc._cache) { rc._cache->append(DataRef(DataRef::DRString, fi->text, QString::null, 0.0, QVariant(stp->value()))); } stp->unlock(); } else { KST::dataObjectList.lock().readLock(); KstDataObjectList::Iterator oi = KST::dataObjectList.findTag(fi->text); KST::dataObjectList.lock().unlock(); if (oi != KST::dataObjectList.end()) { KstPluginPtr fit = kst_cast<KstPlugin>(*oi); if (fit) { fit->readLock(); const QString txtAll = fit->label(rc.precision); fit->unlock(); const QValueList<QString> strList = QStringList::split('\n', txtAll); QValueListConstIterator<QString> last = --(strList.end()); for (QValueListConstIterator<QString> iter = strList.begin(); iter != strList.end(); ++iter) { txt = (*iter); if (iter != last) { if (rc.p) { rc.p->drawText(rc.x, rc.y, txt); } else { rc.ascent = kMax(rc.ascent, -rc.y + rc.fontAscent()); if (-rc.y - rc.fontDescent() < 0) { rc.descent = kMax(rc.descent, rc.fontDescent() + rc.y); } } rc.x += rc.fontWidth(txt); rc.xMax = kMax(rc.xMax, rc.x); rc.x = oldX; rc.y += rc.fontAscent() + rc.fontDescent() + 1; } } if (rc._cache) { rc._cache->append(DataRef(DataRef::DRFit, fi->text, txtAll, rc.precision, QVariant(0.0))); } } } } } } if (rc.p) { rc.p->drawText(rc.x, rc.y, txt); } rc.x += rc.fontWidth(txt); } else if (fi->vector) { QString txt; KST::vectorList.lock().readLock(); KstVectorPtr vp = *KST::vectorList.findTag(fi->text); KST::vectorList.lock().unlock(); if (vp) { if (!fi->expression.isEmpty()) { // Parse and evaluate as an equation bool ok = false; // FIXME: make more efficient: cache the parsed equation const double idx = Equation::interpret(fi->expression.latin1(), &ok); if (ok) { vp->readLock(); const double vVal(vp->value()[int(idx)]); txt = QString::number(vVal, 'g', rc.precision); if (rc._cache) { rc._cache->append(DataRef(DataRef::DRVector, fi->text, fi->expression, idx, QVariant(vVal))); } vp->unlock(); } else { txt = "NAN"; } } } if (rc.p) { rc.p->drawText(rc.x, rc.y, txt); } rc.x += rc.fontWidth(txt); } else if (fi->tab) { const int tabWidth = rc.fontWidth("MMMMMMMM"); const int toSkip = tabWidth - (rc.x - rc.xStart) % tabWidth; if (rc.p && fi->attributes.underline) { const int spaceWidth = rc.fontWidth(" "); const int spacesToSkip = tabWidth / spaceWidth + (tabWidth % spaceWidth > 0 ? 1 : 0); rc.p->drawText(rc.x, rc.y, QString().fill(' ', spacesToSkip)); } rc.x += toSkip; } else { if (rc.p) { #ifdef BENCHMARK QTime t; t.start(); #endif rc.p->drawText(rc.x, rc.y, fi->text); #ifdef BENCHMARK kstdDebug() << "Renderer did draw, time: " << t.elapsed() << endl; #endif } rc.x += rc.fontWidth(fi->text); } if (!rc.p) { // No need to compute ascent and descent when really painting rc.ascent = kMax(rc.ascent, -rc.y + rc.fontAscent()); if (-rc.y - rc.fontDescent() < 0) { rc.descent = kMax(rc.descent, rc.fontDescent() + rc.y); } } int xNext = rc.x; if (fi->group) { renderLabel(rc, fi->group); xNext = rc.x; } if (fi->up) { int xPrev = rc.x; renderLabel(rc, fi->up); xNext = kMax(xNext, rc.x); rc.x = xPrev; } if (fi->down) { renderLabel(rc, fi->down); xNext = kMax(xNext, rc.x); } rc.x = xNext; rc.xMax = kMax(rc.xMax, rc.x); fi = fi->next; } rc.size = oldSize; rc.y = oldY; }
namespace BigFix { static DataRef months[12] = { DataRef( "Jan" ), DataRef( "Feb" ), DataRef( "Mar" ), DataRef( "Apr" ), DataRef( "May" ), DataRef( "Jun" ), DataRef( "Jul" ), DataRef( "Aug" ), DataRef( "Sep" ), DataRef( "Oct" ), DataRef( "Nov" ), DataRef( "Dec" ) }; static uint8_t ReadMonth( DataRef month ) { for ( uint8_t i = 0; i < 12; i++ ) if ( month == months[i] ) return i + 1; throw Error( "Invalid datetime: bad month" ); } static DataRef days[7] = { DataRef( "Sun" ), DataRef( "Mon" ), DataRef( "Tue" ), DataRef( "Wed" ), DataRef( "Thu" ), DataRef( "Fri" ), DataRef( "Sat" ) }; static uint8_t ReadDayOfWeek( DataRef day ) { for ( uint8_t i = 0; i < 7; i++ ) if ( day == days[i] ) return i + 1; throw Error( "Invalid datetime: bad day of week" ); } static void Verify( bool condition ) { if ( !condition ) throw Error( "Invalid datetime" ); } DateTime::DateTime() : m_year( 1970 ) , m_month( 1 ) , m_day( 1 ) , m_dayOfWeek( 4 ) , m_hour( 0 ) , m_minute( 0 ) , m_second( 0 ) { } DateTime::DateTime( DataRef date ) { // Example: Sun, 11 Mar 1984 00:00:00 +0000 Verify( date.Length() == 31 ); m_dayOfWeek = ReadDayOfWeek( date.Slice( 0, 3 ) ); Verify( date.Slice( 3, 2 ) == DataRef( ", " ) ); m_day = ReadAsciiNumber<uint8_t>( date.Slice( 5, 2 ) ); Verify( date[7] == ' ' ); m_month = ReadMonth( date.Slice( 8, 3 ) ); Verify( date[11] == ' ' ); m_year = ReadAsciiNumber<uint32_t>( date.Slice( 12, 4 ) ); Verify( date[16] == ' ' ); m_hour = ReadAsciiNumber<uint8_t>( date.Slice( 17, 2 ) ); Verify( date[19] == ':' ); m_minute = ReadAsciiNumber<uint8_t>( date.Slice( 20, 2 ) ); Verify( date[22] == ':' ); m_second = ReadAsciiNumber<uint8_t>( date.Slice( 23, 2 ) ); Verify( date[25] == ' ' ); Verify( date.Slice( 25, 6 ) == DataRef( " +0000" ) ); } DateTime::DateTime( uint32_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second ) : m_year( year ) , m_month( month ) , m_day( day ) , m_dayOfWeek( dayOfWeek ) , m_hour( hour ) , m_minute( minute ) , m_second( second ) { } #ifdef _WIN32 #define snprintf _snprintf_s #endif std::string DateTime::ToString() const { char buffer[32]; int status = snprintf( buffer, sizeof( buffer ), "%s, %02d %s %04d %02d:%02d:%02d +0000", reinterpret_cast<const char*>( days[m_dayOfWeek - 1].Start() ), static_cast<int>( m_day ), reinterpret_cast<const char*>( months[m_month - 1].Start() ), static_cast<int>( m_year ), static_cast<int>( m_hour ), static_cast<int>( m_minute ), static_cast<int>( m_second ) ); if ( status != 31 ) throw Error( "Failed to convert date to string" ); return buffer; } }
DataRef TwoFish::getDataImpl(void) const { const uint8_t *ptr=convPtrToConst<uint8_t>( impl_->buf_.get() ); return DataRef(ptr, impl_->len_); }
//-------------------------------------------------------- // parse XML tag to Binary data[tree] //-------------------------------------------------------- Bool TLFile::ParseXMLDataTree(TXmlTag& Tag,TBinaryTree& Data) { /* XML examples // root data <Data><u32>100</u32></Data> // put in "translate" child <Data DataRef="translate"><float3>0,40,0</float3></Data> // "Node" data inside "NodeList" data <Data DataRef="NodeList"> <Bool>TRUE</Bool> // written to "NodeList" <Data DataRef="Node"><TRef>ohai</TRef></Data> </Data> */ // read the data ref const TString* pDataRefString = Tag.GetProperty("DataRef"); TRef DataRef( pDataRefString ? *pDataRefString : "" ); // establish the data we're writing data to TPtr<TBinaryTree> pDataChild; // add a child to the node data if it has a ref, otherwise data is added to root of the node if ( DataRef.IsValid() ) { pDataChild = Data.AddChild( DataRef ); // failed to add child data... if ( !pDataChild ) { TLDebug_Break("failed to add child data"); return FALSE; } } // import contents of data TBinaryTree& NodeData = pDataChild ? *pDataChild.GetObjectPointer() : Data; // if the tag has no children (eg. type like <float />) but DOES have data (eg. 1.0) throw up an error and fail // assume the data is malformed and someone has forgotten to add the type specifier. // if something automated has output it and doesnt know the type it should still output it as hex raw data if ( !Tag.GetChildren().GetSize() && Tag.GetDataString().GetLength() > 0 ) { TTempString Debug_String("<Data "); DataRef.GetString( Debug_String ); Debug_String.Append("> tag with no children, but DOES have data inside (eg. 1.0). Missing type specifier? (eg. <flt3>)\n"); Debug_String.Append( Tag.GetDataString() ); TLDebug_Break( Debug_String ); return SyncFalse; } // deal with child tags for ( u32 c=0; c<Tag.GetChildren().GetSize(); c++ ) { TPtr<TXmlTag>& pChildTag = Tag.GetChildren().ElementAt(c); SyncBool TagImportResult = SyncFalse; if ( pChildTag->GetTagName() == "data" ) { // import child data if ( ParseXMLDataTree( *pChildTag, NodeData ) ) TagImportResult = SyncTrue; else TagImportResult = SyncFalse; if ( TagImportResult != SyncTrue ) Data.Debug_PrintTree(); } else { TRef DataTypeRef = TLFile::GetDataTypeFromString( pChildTag->GetTagName() ); // update type of data // gr: DONT do this, if the type is mixed, this overwrites it. Setting the DataTypeHint should be automaticly done when we Write() in ImportBinaryData //NodeData.SetDataTypeHint( DataTypeRef ); TagImportResult = TLFile::ImportBinaryData( pChildTag->GetDataString(), NodeData, DataTypeRef ); // gr: just to check the data hint is being set from the above function... if ( TagImportResult == SyncTrue && !NodeData.GetDataTypeHint().IsValid() && NodeData.GetSize() > 0 ) { TTempString Debug_String("Data imported is missing data type hint after successfull write? We just wrote data type "); DataTypeRef.GetString( Debug_String ); Debug_String.Append(". This can be ignored if the data is mixed types"); TLDebug_Break( Debug_String ); Data.Debug_PrintTree(); } } // failed if ( TagImportResult == SyncFalse ) { TTempString str; str << "failed to import <data> tag \"" << pChildTag->GetTagName() << "\""; TLDebug_Break( str ); return FALSE; } // async is unsupported if ( TagImportResult == SyncWait ) { TLDebug_Break("todo: async Scheme import"); return FALSE; } } return TRUE; }
void OperationContext::setStringData(const String& key, const String& str) { m_data[key] = DataRef(new StringData(str)); }