// --------------------------------------------------------- // CWlanMgmtCommandHandler::ConfigureMulticastGroup // Get the signal quality of the last received packet. // --------------------------------------------------------- // void CWlanMgmtCommandHandler::ConfigureMulticastGroup( TBool aJoinGroup, const TMacAddress& aMulticastAddr ) { DEBUG( "CWlanMgmtCommandHandler::ConfigureMulticastGroup()" ); TInt err = KErrNone; if( aJoinGroup ) { TAddMulticastAddrMsg msg; msg.hdr.oid_id = E802_11_ADD_MULTICAST_ADDR; msg.macAddress = aMulticastAddr; TPckg<TAddMulticastAddrMsg> outbuf( msg ); err = iChannel.ManagementCommand( outbuf, NULL, &iStatus ); } else { TRemoveMulticastAddrMsg msg; msg.hdr.oid_id = E802_11_REMOVE_MULTICAST_ADDR; msg.macAddress = aMulticastAddr; msg.removeAll = EFalse; TPckg<TRemoveMulticastAddrMsg> outbuf( msg ); err = iChannel.ManagementCommand( outbuf, NULL, &iStatus ); } if( err ) { DEBUG1( "ERROR calling RWlanLogicalChannel::ManagementCommand(): %d", err ); TRequestStatus* status = &iStatus; User::RequestComplete( status, err ); } SetActive(); }
string convert_encoding(const string& data, const string& from, const string& to) { if (data.empty()) { return string(); } iconv_t convert_hnd = iconv_open(to.c_str(), from.c_str()); if (convert_hnd == (iconv_t)(-1)) { throw logic_error("unable to create convertion descriptor"); } char* in_ptr = const_cast<char*>(data.c_str()); size_t in_size = data.size(); vector<char> outbuf(6 * data.size()); char* out_ptr = &outbuf[0]; size_t reverse_size = outbuf.size(); size_t result = iconv(convert_hnd, &in_ptr, &in_size, &out_ptr, &reverse_size); iconv_close(convert_hnd); if (result == (size_t)(-1)) { throw logic_error("unable to convert"); } return string(outbuf.data(), outbuf.size() - reverse_size); }
void Processor<sint, sgf2n>::read_shares_from_file(int start_file_posn, int end_file_pos_register, const vector<int>& data_registers) { string filename; filename = "Persistence/Transactions-P" + to_string(P.my_num()) + ".data"; unsigned int size = data_registers.size(); vector< sint > outbuf(size); int end_file_posn = start_file_posn; try { binary_file_io.read_from_file(filename, outbuf, start_file_posn, end_file_posn); for (unsigned int i = 0; i < size; i++) { get_Sp_ref(data_registers[i]) = outbuf[i]; } write_Ci(end_file_pos_register, (long)end_file_posn); } catch (file_missing& e) { cerr << "Got file missing error, will return -2. " << e.what() << endl; write_Ci(end_file_pos_register, (long)-2); } }
void FastcgiRequest::write(std::streambuf *buf) { std::vector<char> outv(4096); fcgi_streambuf outbuf(fcgiRequest_.out, &outv[0], outv.size()); std::ostream os(&outbuf); os << buf; }
void code(lzma_stream &stream, std::istream &in, std::ostream &out) { std::vector<char> inbuf(chunksize, 0), outbuf(chunksize, 0); while (true) { stream.next_in = reinterpret_cast<uint8_t *>(&inbuf[0]); in.read(&inbuf[0], inbuf.size()); std::streamsize insize = in.gcount(); stream.avail_in = insize; stream.total_in = 0; do { stream.total_out = 0; stream.next_out = reinterpret_cast<uint8_t *>(&outbuf[0]); stream.avail_out = outbuf.size(); lzma_ret retval = lzma_code(&stream, insize == 0 ? LZMA_FINISH : LZMA_RUN); if (retval == LZMA_STREAM_END) { if (stream.avail_in != 0) throw compress_error{"Unprocessed input remaining in stream"}; out.write(&outbuf[0], stream.total_out); lzma_end(&stream); return; } if (retval != LZMA_OK) throw compress_error{"Stream encoding failed"}; //std::cout << "Retrieved " << stream.total_in << " bytes and output " << stream.total_out << " bytes\n"; out.write(&outbuf[0], stream.total_out); } while (stream.total_out > 0); } }
std::string convert(const std::string& input) const { std::string converted{}; char* p_in = const_cast<char*>(input.c_str()); const std::size_t in_length = input.length(); std::size_t in_left = in_length; for (;;) { static const std::size_t outbuf_capacity = 10; std::vector<char> outbuf(outbuf_capacity, 0); char* p_out = &outbuf[0]; std::size_t out_left = outbuf_capacity; errno = 0; const std::size_t result = ::iconv(m_conversion_descriptor, &p_in, &in_left, &p_out, &out_left); if (result == static_cast<std::size_t>(-1) && errno == EINVAL) break; converted.append(&outbuf[0], outbuf_capacity - out_left); if (errno == EILSEQ) { assert(in_left > 0); converted += '?'; ++p_in; --in_left; } if (in_left == 0) break; } return converted; }
QByteArray MIMECodec::encodeBase64(const QByteArray &buf, bool endNewLine, bool endAllNewLines) { const unsigned wrap = 76; unsigned bufSize = buf.size(); #ifdef _QTWIN_ int index = 0, lindex = 0, oindex = 0, outsize = ((bufSize + 2) / 3) * 4; #else unsigned index = 0, lindex = 0, oindex = 0, outsize = ((bufSize + 2) / 3) * 4; #endif outsize += (outsize + wrap - 1 / wrap) * 2; QByteArray outbuf(outsize); unsigned char e1 = 0, e2 = 0, e3 = 0, e4 = 0; unsigned char c1 = 0, c2 = 0, c3 = 0; while (index < bufSize) { c1 = buf[index]; c2 = index < bufSize - 1 ? buf[index + 1] : 0; c3 = index < bufSize - 2 ? buf[index + 2] : 0; e1 = c1 / 4; e2 = (c1 & 3) * 16 + (c2 & 240) / 16; e3 = (c2 & 15) * 4 + (c3 & 192) / 64; e4 = c3 & 63; if (lindex == wrap && endAllNewLines) { outbuf[oindex++] = '\n'; lindex = 0; } outbuf[oindex++] = base64table[e1]; outbuf[oindex++] = base64table[e2]; lindex += 2; if (lindex == wrap && endAllNewLines) { outbuf[oindex++] = '\n'; lindex = 0; } outbuf[oindex++] = base64table[e3]; outbuf[oindex++] = base64table[e4]; lindex += 2; index += 3; } int r = (bufSize) % 3; if (r == 1) outbuf[oindex - 2] = '='; if (r) outbuf[oindex - 1] = '='; if (endNewLine) outbuf[oindex++] = '\n'; outbuf[oindex] = '\0'; outbuf.truncate(oindex + 1); return outbuf; }
bool utf8ToUtf16( const char* source, std::wstring& output ) { int len = ::MultiByteToWideChar( CP_UTF8, 0, source, -1, NULL, 0); std::vector<wchar_t> outbuf( len+1, 0 ); int ret = ::MultiByteToWideChar( CP_UTF8, 0, source, -1, &(outbuf[0]), len ); if( ret ) { outbuf[ret] = L'\0'; output.assign( &(outbuf[0]) ); return true; } return false; }
bool utf16ToMbs( const wchar_t* source, std::string& output ) { int len = ::WideCharToMultiByte( CP_ACP, 0, source, -1, NULL, 0, NULL, NULL ); std::vector<char> outbuf( len+1, 0 ); int ret = ::WideCharToMultiByte( CP_ACP, 0, source, -1, &(outbuf[0]), len, NULL, NULL ); if( ret ) { outbuf[ret] = L'\0'; output.assign( &(outbuf[0]) ); return true; } return false; }
Buffer ChunkBlockInfo::ToBuffer() { int tmp_buffer_size = 2*1024*32; int buf_len = 0; byte *tmp = new byte[tmp_buffer_size]; *(u_int*)(tmp+buf_len) = max_subpiece_index_+1; buf_len += 4; for(std::map<u_short,ChunkPieceInfo::p>::const_iterator pieceit = piece_info_set_.begin();pieceit!=piece_info_set_.end();pieceit++) { //piece index size_t piece_index = pieceit->first; *(u_short*)(tmp+buf_len) = piece_index; buf_len += 2; //piece len ChunkPieceInfo::p chunk_piece_info = pieceit->second; Buffer piecebuf; if(!chunk_piece_info) { piecebuf = ChunkPieceInfo::NullPieceToBuffer(); } else { piecebuf = pieceit->second->ToBuffer(); } if(buf_len+piecebuf.length_+1024>tmp_buffer_size) { tmp_buffer_size = tmp_buffer_size*2; byte *tmp2 = tmp; tmp = new byte[tmp_buffer_size]; memcpy(tmp,tmp2,buf_len); delete[] tmp2; } //piecebuffer 长度、内容 *(int*)(tmp+buf_len) = piecebuf.length_; buf_len += 4; memcpy((tmp+buf_len),piecebuf.data_.get(),piecebuf.length_); buf_len += piecebuf.length_; } Buffer outbuf(tmp,buf_len); delete[] tmp; return outbuf; }
int prompt(editor_t *ed, char *msg, int selection) { int maxlen, len, ch; char *buf = ed->linebuf; gotoxy(ed, 0, ed->lines); outstr(ed, STATUS_COLOR); outstr(ed, msg); outstr(ed, CLREOL); len = 0; maxlen = ed->cols - strlen(msg) - 1; if (selection) { len = get_selected_text(ed, buf, maxlen); outbuf(ed, buf, len); } for (;;) { ch = getkey(ed); if (ch == KEY_ESC) { return 0; } else if (ch == KEY_ENTER) { buf[len] = 0; return len > 0; } else if (ch == KEY_BACKSPACE) { if (len > 0) { outstr(ed, "\b \b"); len--; } } else if (ch >= ' ' && ch < 0x100 && len < maxlen) { outch(ed, ch); buf[len++] = ch; } } }
// --------------------------------------------------------- // CWlanMgmtCommandHandler::GetLastRCPI // Get the signal quality of the last received packet. // --------------------------------------------------------- // void CWlanMgmtCommandHandler::GetLastRCPI( TUint32& aRCPI ) { DEBUG( "CWlanMgmtCommandHandler::GetLastRCPI()" ); TGetLastRcpiMsg msg; msg.hdr.oid_id = E802_11_GET_LAST_RCPI; TPckg<TGetLastRcpiMsg> outbuf( msg ); TPckg<TUint32> inbuf( aRCPI ); iBuffer.Set( inbuf ); TInt err = iChannel.ManagementCommand( outbuf, &iBuffer, &iStatus ); if( err ) { DEBUG1( "ERROR calling RWlanLogicalChannel::ManagementCommand(): %d", err ); TRequestStatus* status = &iStatus; User::RequestComplete( status, err ); } SetActive(); }
void run(T* buf) { cl_int err; cl::Buffer outbuf( m_context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, N*N*sizeof(T), buf, &err); checkErr(err, "Buffer::Buffer()"); err = m_kernel.setArg(0, outbuf); checkErr(err, "Kernel::setArg(0)"); err = m_kernel.setArg(1, N); checkErr(err, "Kernel::setArg(1)"); err = m_kernel.setArg(2, depth); checkErr(err, "Kernel::setArg(2)"); err = m_kernel.setArg(3, escape2); checkErr(err, "Kernel::setArg(3)"); cl::Event event; err = m_cmdq.enqueueNDRangeKernel( m_kernel, cl::NullRange, cl::NDRange(N*N), cl::NDRange(N, 1), NULL, &event); checkErr(err, "ComamndQueue::enqueueNDRangeKernel()"); event.wait(); err = m_cmdq.enqueueReadBuffer( outbuf, CL_TRUE, 0, N*N*sizeof(T), buf); checkErr(err, "ComamndQueue::enqueueReadBuffer()"); }
// --------------------------------------------------------- // CWlanMgmtCommandHandler::GetPacketStatistics // --------------------------------------------------------- // void CWlanMgmtCommandHandler::GetPacketStatistics( TStatisticsResponse& aStatistics ) { DEBUG( "CWlanMgmtCommandHandler::GetPacketStatistics()" ); TGetFrameStatisticsMsg msg; msg.hdr.oid_id = E802_11_GET_FRAME_STATISTICS; TPckg<TGetFrameStatisticsMsg> outbuf( msg ); TPckg<TStatisticsResponse> inbuf( aStatistics ); iBuffer.Set( inbuf ); TInt err = iChannel.ManagementCommand( outbuf, &iBuffer, &iStatus ); if( err ) { DEBUG1( "ERROR calling RWlanLogicalChannel::ManagementCommand(): %d", err ); TRequestStatus* status = &iStatus; User::RequestComplete( status, err ); } SetActive(); }
bool MemCacheServerHandler::GetPlatInfo(const string& platID, std::string& dist) { if (g_pInst == NULL || !g_pInst->CanUse()) { return false; } // if (uid != 2928562105553271) // { // return false; // } int dbid = g_pInst->GetServerId(platID); TCRDB* pConn = g_pInst->GetDB(dbid); if (pConn == NULL) { g_pInst->SetEnbale(false); LOG4CXX_ERROR(logger,"memcache_get_connect_null_error"); return false; } int len = 0; int klen = platID.length(); char* buffer = (char*)tcrdbget(pConn, platID.c_str(), klen, &len); if (buffer == NULL) { int ecode = tcrdbecode(pConn); if (ecode != TTENOREC) { g_pInst->SetEnbale(false); LOG4CXX_ERROR(logger,"memcache_get_ecode_error"); } return false; } std::string outbuf(buffer, len); dist = outbuf; free(buffer); //g_pInst->GetCounter().increase("mem_get_user"); return true; }
int CallCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); std::vector <char> outbuf( mOutBufSize ); HMODULE dll = LoadLibrary( mDLL.c_str() ); if ( dll == NULL ) { CSVTHROW( "LoadLibrary call on " << mDLL << " failed" ); } mFunc = (FuncType) GetProcAddress( dll, mFuncName.c_str() ); if ( mFunc == NULL ) { CSVTHROW( "Cannot load function " << mFuncName << "from DLL " << mDLL ); } IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( ! Skip( io, row ) ) { continue; } if ( Pass( io, row ) ) { io.WriteRow( row ); continue; } int rv = CallOnFields( row, &outbuf[0] ); if ( rv == 0 ) { io.WriteRow( row ); } else if ( rv > 0 ) { CSVTHROW( mFuncName << " returned error code " << rv ); } else { // do nothing - negative values just mean skip output } } return 0; }
int main(int argc, char *argv[]) { const int test_size = 128; std::random_device rd; std::seed_seq s{ rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd() }; std::mt19937 mt(s); EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kern = cl->buildKernel("test.cl", "test"); std::vector<uint32_t> inbuf(test_size*4), outbuf(test_size), compare(test_size); std::generate(inbuf.begin(), inbuf.end(), mt); std::cout << "Running CL implementation" << std::endl; kern->in(inbuf.size(), &inbuf[0]); kern->out(outbuf.size(), &outbuf[0]); size_t global_size[] = { test_size }; kern->run(1, global_size, nullptr); delete kern; std::cout << "Running local implementation" << std::endl; for (int i = 0; i < compare.size(); ++i) { compare[i] = inbuf[i] ^ inbuf[i + 1] ^ inbuf[i + 2] ^ inbuf[i + 3]; } std::cout << "Comparing CL test with local implementation" << std::endl; for (int i = 0; i < compare.size(); ++i) { if (outbuf[i] != compare[i]) { std::cout << "Error in index " << i << " " << outbuf[i] << " != " << compare[i] << std::endl; } } return 0; }
void display_line(editor_t *ed, int pos, int fullline) { int hilite = 0; int col = 0; int margin = ed->margin; int maxcol = ed->cols + margin; char *bufptr = ed->linebuf; char *p = text_ptr(ed, pos); int selstart, selend, ch; char *s; get_selection(ed, &selstart, &selend); while (col < maxcol) { if (margin == 0) { if (!hilite && pos >= selstart && pos < selend) { for (s = SELECT_COLOR; *s; s++) *bufptr++ = *s; hilite = 1; } else if (hilite && pos >= selend) { for (s = TEXT_COLOR; *s; s++) *bufptr++ = *s; hilite = 0; } } if (p == ed->end) break; ch = *p; if (ch == '\r' || ch == '\n') break; if (ch == '\t') { int spaces = TABSIZE - col % TABSIZE; while (spaces > 0 && col < maxcol) { if (margin > 0) { margin--; } else { *bufptr++ = ' '; } col++; spaces--; } } else { if (margin > 0) { margin--; } else { *bufptr++ = ch; } col++; } if (++p == ed->gap) p = ed->rest; pos++; } if (col < maxcol) { for (s = CLREOL; *s; s++) *bufptr++ = *s; if (fullline) { memcpy(bufptr, "\r\n", 2); bufptr += 2; } } if (hilite) { for (s = TEXT_COLOR; *s; s++) *bufptr++ = *s; } outbuf(ed, ed->linebuf, bufptr - ed->linebuf); }
bool CLocalPath::SetPath(const wxString& path, wxString* file /*=0*/) { // This function ensures that the path is in canonical form on success. if (path.empty()) { m_path.clear(); return false; } std::list<wxChar*> segments; // List to store the beginnings of segments const wxChar* in = path.c_str(); #ifdef __WXMSW__ if (path == _T("\\")) { m_path = _T("\\"); if (file) file->clear(); return true; } wxChar* out; if (*in == '\\') { // possibly UNC in++; if (*in++ != '\\') { m_path.clear(); return false; } wxChar* buf = m_path.GetWriteBuf(path.Len() + 2); out = buf; *out++ = '\\'; *out++ = '\\'; // UNC path while (*in) { if (*in == '/' || *in == '\\') break; *out++ = *in++; } *out++ = path_separator; if (out - buf <= 3) { // not a valid UNC path *buf = 0; m_path.UngetWriteBuf(); return false; } segments.push_back(out); } else if ((*in >= 'a' && *in <= 'z') || (*in >= 'A' || *in <= 'Z')) { // Regular path wxChar* buf = m_path.GetWriteBuf(path.Len() + 2); out = buf; *out++ = *in++; if (*in++ != ':') { *buf = 0; m_path.UngetWriteBuf(); return false; } *out++ = ':'; if (*in != '/' && *in != '\\' && *in) { *buf = 0; m_path.UngetWriteBuf(); return false; } *out++ = path_separator; segments.push_back(out); } else { m_path.clear(); return false; } #else if (*in++ != '/') { // SetPath only accepts absolute paths m_path.clear(); return false; } wxStringBuffer outbuf(m_path, path.Len() + 2); wxChar* out = outbuf; *out++ = '/'; segments.push_back(out); #endif enum _last { separator, dot, dotdot, segment }; enum _last last = separator; while (*in) { if (*in == '/' #ifdef __WXMSW__ || *in == '\\' #endif ) { in++; if (last == separator) { // /foo//bar is equal to /foo/bar continue; } else if (last == dot) { // /foo/./bar is equal to /foo/bar last = separator; out = segments.back(); continue; } else if (last == dotdot) { last = separator; // Go two segments back if possible if (segments.size() > 1) segments.pop_back(); wxASSERT(!segments.empty()); out = segments.back(); continue; } // Ordinary segment just ended. *out++ = path_separator; segments.push_back(out); last = separator; continue; } else if (*in == '.') { if (last == separator) last = dot; else if (last == dot) last = dotdot; else if (last == dotdot) last = segment; } else last = segment; *out++ = *in++; } if (last == dot) out = segments.back(); else if (last == dotdot) { if (segments.size() > 1) segments.pop_back(); out = segments.back(); } else if (last == segment) { if (file) { *out = 0; out = segments.back(); *file = out; } else *out++ = path_separator; } *out = 0; return true; }
NS_IMETHODIMP nsIndexedToHTML::OnIndexAvailable(nsIRequest *aRequest, nsISupports *aCtxt, nsIDirIndex *aIndex) { nsresult rv; if (!aIndex) return NS_ERROR_NULL_POINTER; nsString pushBuffer; pushBuffer.AppendLiteral("<tr"); nsXPIDLString description; aIndex->GetDescription(getter_Copies(description)); if (description.First() == char16_t('.')) pushBuffer.AppendLiteral(" class=\"hidden-object\""); pushBuffer.AppendLiteral(">\n <td sortable-data=\""); // The sort key is the name of the item, prepended by either 0, 1 or 2 // in order to group items. uint32_t type; aIndex->GetType(&type); switch (type) { case nsIDirIndex::TYPE_SYMLINK: pushBuffer.AppendInt(0); break; case nsIDirIndex::TYPE_DIRECTORY: pushBuffer.AppendInt(1); break; case nsIDirIndex::TYPE_FILE: case nsIDirIndex::TYPE_UNKNOWN: pushBuffer.AppendInt(2); break; } char16_t* escaped = nsEscapeHTML2(description.get(), description.Length()); pushBuffer.Append(escaped); pushBuffer.AppendLiteral("\"><a class=\""); switch (type) { case nsIDirIndex::TYPE_DIRECTORY: pushBuffer.AppendLiteral("dir"); break; case nsIDirIndex::TYPE_SYMLINK: pushBuffer.AppendLiteral("symlink"); break; case nsIDirIndex::TYPE_FILE: case nsIDirIndex::TYPE_UNKNOWN: pushBuffer.AppendLiteral("file"); break; } pushBuffer.AppendLiteral("\""); // Truncate long names to not stretch the table //XXX this should be left to the stylesheet (bug 391471) nsString escapedShort; if (description.Length() > 71) { nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); nsCOMPtr<nsIURI> uri; rv = channel->GetURI(getter_AddRefs(uri)); if (NS_FAILED(rv)) return rv; //XXX this potentially truncates after a combining char (bug 391472) nsXPIDLString descriptionAffix; descriptionAffix.Assign(description); descriptionAffix.Cut(0, descriptionAffix.Length() - 25); if (NS_IS_LOW_SURROGATE(descriptionAffix.First())) descriptionAffix.Cut(0, 1); description.Truncate(std::min<uint32_t>(71, description.Length() - 28)); if (NS_IS_HIGH_SURROGATE(description.Last())) description.Truncate(description.Length() - 1); escapedShort.Adopt(nsEscapeHTML2(description.get(), description.Length())); escapedShort.Append(mEscapedEllipsis); // add ZERO WIDTH SPACE (U+200B) for wrapping escapedShort.AppendLiteral("​"); nsString tmp; tmp.Adopt(nsEscapeHTML2(descriptionAffix.get(), descriptionAffix.Length())); escapedShort.Append(tmp); pushBuffer.AppendLiteral(" title=\""); pushBuffer.Append(escaped); pushBuffer.AppendLiteral("\""); } if (escapedShort.IsEmpty()) escapedShort.Assign(escaped); nsMemory::Free(escaped); pushBuffer.AppendLiteral(" href=\""); nsXPIDLCString loc; aIndex->GetLocation(getter_Copies(loc)); nsXPIDLCString encoding; rv = mParser->GetEncoding(getter_Copies(encoding)); if (NS_FAILED(rv)) return rv; // Don't byte-to-Unicode conversion here, it is lossy. loc.SetLength(nsUnescapeCount(loc.BeginWriting())); // need to escape links nsAutoCString locEscaped; // Adding trailing slash helps to recognize whether the URL points to a file // or a directory (bug #214405). if ((type == nsIDirIndex::TYPE_DIRECTORY) && (loc.Last() != '/')) { loc.Append('/'); } // now minimally re-escape the location... uint32_t escFlags; // for some protocols, we expect the location to be absolute. // if so, and if the location indeed appears to be a valid URI, then go // ahead and treat it like one. if (mExpectAbsLoc && NS_SUCCEEDED(net_ExtractURLScheme(loc, nullptr, nullptr, nullptr))) { // escape as absolute escFlags = esc_Forced | esc_OnlyASCII | esc_AlwaysCopy | esc_Minimal; } else { // escape as relative // esc_Directory is needed because directories have a trailing slash. // Without it, the trailing '/' will be escaped, and links from within // that directory will be incorrect escFlags = esc_Forced | esc_OnlyASCII | esc_AlwaysCopy | esc_FileBaseName | esc_Colon | esc_Directory; } NS_EscapeURL(loc.get(), loc.Length(), escFlags, locEscaped); // esc_Directory does not escape the semicolons, so if a filename // contains semicolons we need to manually escape them. // This replacement should be removed in bug #473280 locEscaped.ReplaceSubstring(";", "%3b"); nsAutoString utf16URI; if (encoding.EqualsLiteral("UTF-8")) { // Try to convert non-ASCII bytes to Unicode using UTF-8 decoder. nsCOMPtr<nsIUnicodeDecoder> decoder = mozilla::dom::EncodingUtils::DecoderForEncoding("UTF-8"); decoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal); int32_t len = locEscaped.Length(); int32_t outlen = 0; rv = decoder->GetMaxLength(locEscaped.get(), len, &outlen); if (NS_FAILED(rv)) { return rv; } nsAutoArrayPtr<char16_t> outbuf(new char16_t[outlen]); rv = decoder->Convert(locEscaped.get(), &len, outbuf, &outlen); // Use the result only if the sequence is valid as UTF-8. if (rv == NS_OK) { utf16URI.Append(outbuf, outlen); } } if (utf16URI.IsEmpty()) { // Escape all non-ASCII bytes to preserve the raw value. nsAutoCString outstr; NS_EscapeURL(locEscaped, esc_AlwaysCopy | esc_OnlyNonASCII, outstr); CopyASCIItoUTF16(outstr, utf16URI); } nsString htmlEscapedURL; htmlEscapedURL.Adopt(nsEscapeHTML2(utf16URI.get(), utf16URI.Length())); pushBuffer.Append(htmlEscapedURL); pushBuffer.AppendLiteral("\">"); if (type == nsIDirIndex::TYPE_FILE || type == nsIDirIndex::TYPE_UNKNOWN) { pushBuffer.AppendLiteral("<img src=\"moz-icon://"); int32_t lastDot = locEscaped.RFindChar('.'); if (lastDot != kNotFound) { locEscaped.Cut(0, lastDot); NS_ConvertUTF8toUTF16 utf16LocEscaped(locEscaped); nsString htmlFileExt; htmlFileExt.Adopt(nsEscapeHTML2(utf16LocEscaped.get(), utf16LocEscaped.Length())); pushBuffer.Append(htmlFileExt); } else { pushBuffer.AppendLiteral("unknown"); } pushBuffer.AppendLiteral("?size=16\" alt=\""); nsXPIDLString altText; rv = mBundle->GetStringFromName(MOZ_UTF16("DirFileLabel"), getter_Copies(altText)); if (NS_FAILED(rv)) return rv; AppendNonAsciiToNCR(altText, pushBuffer); pushBuffer.AppendLiteral("\">"); } pushBuffer.Append(escapedShort); pushBuffer.AppendLiteral("</a></td>\n <td"); if (type == nsIDirIndex::TYPE_DIRECTORY || type == nsIDirIndex::TYPE_SYMLINK) { pushBuffer.AppendLiteral(">"); } else { int64_t size; aIndex->GetSize(&size); if (uint64_t(size) != UINT64_MAX) { pushBuffer.AppendLiteral(" sortable-data=\""); pushBuffer.AppendInt(size); pushBuffer.AppendLiteral("\">"); nsAutoString sizeString; FormatSizeString(size, sizeString); pushBuffer.Append(sizeString); } else { pushBuffer.AppendLiteral(">"); } } pushBuffer.AppendLiteral("</td>\n <td"); PRTime t; aIndex->GetLastModified(&t); if (t == -1) { pushBuffer.AppendLiteral("></td>\n <td>"); } else { pushBuffer.AppendLiteral(" sortable-data=\""); pushBuffer.AppendInt(static_cast<int64_t>(t)); pushBuffer.AppendLiteral("\">"); nsAutoString formatted; mDateTime->FormatPRTime(nullptr, kDateFormatShort, kTimeFormatNone, t, formatted); AppendNonAsciiToNCR(formatted, pushBuffer); pushBuffer.AppendLiteral("</td>\n <td>"); mDateTime->FormatPRTime(nullptr, kDateFormatNone, kTimeFormatSeconds, t, formatted); // use NCR to show date in any doc charset AppendNonAsciiToNCR(formatted, pushBuffer); } pushBuffer.AppendLiteral("</td>\n</tr>"); return FormatInputStream(aRequest, aCtxt, pushBuffer); }
int main (int argc, char **argv) try { edmplugin::PluginManager::configure(edmplugin::standard::config()); if (argc < 3) { std::cerr << "usage: " << argv[0] << " INFILE OUTFILE...\n"; return EXIT_FAILURE; } std::shared_ptr<Storage> is; std::vector<std::unique_ptr<Storage> > os(argc-2); std::vector<std::thread> threads; bool readThreadActive = true; bool writeThreadActive = true; IOOffset size = -1; StorageFactory::getToModify ()->enableAccounting(true); bool exists = StorageFactory::getToModify ()->check(argv [1], &size); std::cerr << "input file exists = " << exists << ", size = " << size << "\n"; if (! exists) return EXIT_SUCCESS; try { is = StorageFactory::getToModify ()->open (argv [1]); if (readThreadActive) threads.emplace_back(&readThread,is.get()); } catch (cms::Exception &e) { std::cerr << "error in opening input file " << argv[1] << ":\n" << e.explainSelf() << std::endl; return EXIT_FAILURE; } // open output files and create threads, one thread per output for (int i=0; i < argc-2; i++) try { os[i]= StorageFactory::getToModify ()->open (argv[i+2], IOFlags::OpenWrite | IOFlags::OpenCreate | IOFlags::OpenTruncate); if (writeThreadActive) threads.emplace_back(&writeThread,os[i].get()); } catch (cms::Exception &e) { std::cerr << "error in opening output file " << argv[i+2] << ":\n" << e.explainSelf() << std::endl; return EXIT_FAILURE; } std::vector<char> inbuf (1048576); std::vector<char> outbuf(1048576); IOSize n; while ((n = readThreadActive ? inbox.get(inbuf) : is->read(&inbuf[0],inbuf.size()))) { //free reading thread inbuf.swap(outbuf); // wait threads have finished to write // drop buffer in thread if (writeThreadActive) { if (! dropbox.set(outbuf,n)) break; } else for (size_t i = 0; i < os.size(); i++) os[i]->write(&outbuf[0],n); } std::cout << "main end reading" << std::endl; // tell thread to end inbuf.clear(); if (readThreadActive) inbox.get(inbuf); if (writeThreadActive) dropbox.set(outbuf, 0); if (writeThreadActive || readThreadActive) { for(auto& t: threads) { t.join(); } } std::cout << StorageAccount::summaryText(true) << std::endl; return EXIT_SUCCESS; } catch(cms::Exception const& e) { std::cerr << e.explainSelf() << std::endl; return EXIT_FAILURE; } catch(std::exception const& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; }
static void outstr(editor_t *ed, char *str) { outbuf(ed, str, strlen(str)); }
QByteArray MIMECodec::decodeBase64(const QByteArray &buf) { if (buf.isEmpty() || buf.size() < 4) return QByteArray(); unsigned bufSize = buf.size(); QByteArray outbuf(((bufSize + 3) / 4) * 3); const char *ptrBuf = buf.data(); #ifdef _QTWIN_ int index = 0, oindex = 0; #else unsigned index = 0, oindex = 0; #endif unsigned char e1 = 0, e2 = 0, e3 = 0, e4 = 0; unsigned char c1 = 0, c2 = 0, c3 = 0; const QCString table(base64table); int revbase64table[256]; { for (int i = 0; i < 256; i++) revbase64table[i] = -1; } { for (int i = 65; i < 91; i++) revbase64table[i] = i - 65; } { for (int i = 97; i < 123; i++) revbase64table[i] = i - 71; } { for (int i = 48; i < 58; i++) revbase64table[i] = i + 4; } revbase64table[43] = 62; revbase64table[47] = 63; revbase64table[61] = 64; while (index < bufSize) { if (revbase64table[(int)ptrBuf[index] ] == -1) { index++; } else { e1 = ptrBuf[index++]; e2 = index < bufSize ? ptrBuf[index++] : 'A'; e3 = index < bufSize ? ptrBuf[index++] : 'A'; e4 = index < bufSize ? ptrBuf[index++] : 'A'; e1 = revbase64table[e1]; e2 = revbase64table[e2]; e3 = revbase64table[e3]; e4 = revbase64table[e4]; c1 = e1 * 4 + (e2 & 48) / 16; c2 = (e2 & 15) * 16 + (e3 & 60) / 4; c3 = (e3 & 3) * 64 + e4; outbuf[oindex++] = c1; outbuf[oindex++] = c2; outbuf[oindex++] = c3; } } unsigned last = 0; for (unsigned i = bufSize - 1; i; i--) if (revbase64table[(int)ptrBuf[i] ] != -1) { last = i; break; } if (ptrBuf[last - 1] == '=' && ptrBuf[last] == '=') oindex--; else if (ptrBuf[last] != '=') oindex++; // do we need it ?? if (outbuf.size() >= oindex) outbuf[oindex - 1] = '\0'; outbuf.truncate(oindex - 1); return outbuf; }
void FfmpegEncoder::writeRawFrame(const AVFrame* picture) { GVX_TRACE(__PRETTY_FUNCTION__); // FIXME We'd like to have a way to either (1) compute what the // maximum necessary itsOutbufSize would be for our given // framerate+bitrate, or (2) get a chance to retry writing a given // frame if it is truncated. However, we have no programmatic way of // knowing whether a given frame gets truncated (all we see is that // ffmpeg prints "encoded frame too large" on stderr), but even then // the return value from avcodec_encode_video() is less than our // itsOutbufSize (although for a "too large" frame we can see // that the return value is clearly higher than usual). Also, it's // hard to determine a hard upper limit on the bufsize, even given // the framerate and bitrate, because the bitrate is only achieved // on /average/ -- so, any particular frame might be much larger // (e.g., 10x or 100x) than the average frame size. So, given all // that, our current approach is just to leave the buffer size up to // the user via the --output-mpeg-bufsize command-line option. // NOTE: it might seem extravagent to allocate+deallocate these // buffers (outbuf, and picture_buf in writeRGB()) for every single // frame that is written; however, profiling shows that this // accounts for only about 2% of the total time spent in // writeFrame(). The alternatives, both with their own // disadvantages, would be (1) have separate buffers allocated once // per object; however this would be expensive in overall memory // usage if we had multiple mpeg streams open at once; or (2) have // static buffers shared by all objects; however, this would require // some form of between-object synchronization in the case of // multi-threading which could be cpu-expensive both for the // locking+unlocking and would also waste time waiting to acquire // the lock for access to the shared buffers. rutz::fixed_block<byte> outbuf(itsOutbufSize); const int out_size = avcodec_encode_video(&itsContext, &outbuf[0], outbuf.size(), picture); if (out_size < 0) LFATAL("error during avcodec_encode_video()"); if (out_size > 0) { itsFrameSizeRange.merge(out_size); if (itsUseFormatContext) { #ifdef INVT_FFMPEG_HAS_FORMATCONTEXT_FUNCTIONS AVPacket pkt; av_init_packet(&pkt); pkt.pts= av_rescale_q(itsContext.coded_frame->pts, itsContext.time_base, itsAVStream->time_base); if(itsContext.coded_frame->key_frame) #ifdef PKT_FLAG_KEY pkt.flags |= PKT_FLAG_KEY; #else pkt.flags |= AV_PKT_FLAG_KEY; #endif pkt.stream_index= itsAVStream->index; pkt.data= &outbuf[0]; pkt.size= out_size; /* write the compressed frame in the media file */ av_write_frame(itsFormatContext, &pkt); #else LFATAL("New a new version of ffmpeg for this option"); #endif } else { fwrite(&outbuf[0], 1, out_size, itsFile); } } LDEBUG("itsOutbufSize=%d, out_size=%d, frameSizeRange=[%d..%d]", itsOutbufSize, out_size, itsFrameSizeRange.min(), itsFrameSizeRange.max()); LDEBUG("encoded frame [zero-based] %d (%d delayed frames pending)", itsFrameNumber, // to compute the number of pending "delayed frames", we // subtract the AVCodecContext's frame number from our own, // except that there is an offset of 2 -- one because // AVCodecContext counts from 1, while we count from zero, and // another because AVCodecContext's counter reports the number // of the NEXT frame to be written, while itsFrameNumber is // the number of the frame that has just been written itsFrameNumber - (itsContext.frame_number - 2)); ++itsFrameNumber; }
void VDCreateTestPal8Video(VDGUIHandle h) { CPUEnableExtensions(CPUCheckForExtensions()); try { tVDInputDrivers inputDrivers; std::vector<int> xlat; VDGetInputDrivers(inputDrivers, IVDInputDriver::kF_Video); const VDStringW filter(VDMakeInputDriverFileFilter(inputDrivers, xlat)); const VDFileDialogOption opt[]={ { VDFileDialogOption::kSelectedFilter }, 0 }; int optval[1]={0}; const VDStringW srcfile(VDGetLoadFileName('pl8s', h, L"Choose source file", filter.c_str(), NULL, opt, optval)); if (srcfile.empty()) return; IVDInputDriver *pDrv; int filtidx = xlat[optval[0] - 1]; if (filtidx < 0) pDrv = VDAutoselectInputDriverForFile(srcfile.c_str(), IVDInputDriver::kF_Video); else { tVDInputDrivers::iterator itDrv(inputDrivers.begin()); std::advance(itDrv, filtidx); pDrv = *itDrv; } vdrefptr<InputFile> pIF(pDrv->CreateInputFile(0)); pIF->Init(srcfile.c_str()); const VDStringW dstfile(VDGetSaveFileName('pl8d', h, L"Choose destination 8-bit file", L"Audio-video interleaved (*.avi)\0*.avi\0All files\0*.*", L"avi", NULL, NULL)); if (dstfile.empty()) return; vdrefptr<IVDVideoSource> pVS; pIF->GetVideoSource(0, ~pVS); IVDStreamSource *pVSS = pVS->asStream(); const VDPosition frames = pVSS->getLength(); if (!pVS->setTargetFormat(nsVDPixmap::kPixFormat_XRGB8888)) throw MyError("Cannot set decompression format to 32-bit."); vdautoptr<IVDMediaOutputAVIFile> pOut(VDCreateMediaOutputAVIFile()); IVDMediaOutputStream *pVSOut = pOut->createVideoStream(); const VDPixmap& pxsrc = pVS->getTargetFormat(); const uint32 rowbytes = (pxsrc.w+3) & ~3; AVIStreamHeader_fixed hdr; hdr.fccType = 'sdiv'; hdr.fccHandler = 0; hdr.dwFlags = 0; hdr.wPriority = 0; hdr.wLanguage = 0; hdr.dwScale = pVSS->getStreamInfo().dwScale; hdr.dwRate = pVSS->getStreamInfo().dwRate; hdr.dwStart = 0; hdr.dwLength = 0; hdr.dwInitialFrames = 0; hdr.dwSuggestedBufferSize = 0; hdr.dwQuality = -1; hdr.dwSampleSize = 0; hdr.rcFrame.left = 0; hdr.rcFrame.top = 0; hdr.rcFrame.right = (short)pxsrc.w; hdr.rcFrame.bottom = (short)pxsrc.h; pVSOut->setStreamInfo(hdr); vdstructex<BITMAPINFOHEADER> bih; bih.resize(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*252); bih->biSize = sizeof(BITMAPINFOHEADER); bih->biWidth = pxsrc.w; bih->biHeight = pxsrc.h; bih->biPlanes = 1; bih->biBitCount = 8; bih->biCompression = BI_RGB; bih->biSizeImage = rowbytes*pxsrc.h; bih->biXPelsPerMeter = 0; bih->biYPelsPerMeter = 0; bih->biClrUsed = 252; bih->biClrImportant = 252; RGBQUAD *pal = (RGBQUAD *)((char *)bih.data() + sizeof(BITMAPINFOHEADER)); for(int i=0; i<252; ++i) { pal[i].rgbRed = (BYTE)((i/42)*51); pal[i].rgbGreen = (BYTE)((((i/6)%7)*85)>>1); pal[i].rgbBlue = (BYTE)((i%6)*51); pal[i].rgbReserved = 0; } pVSOut->setFormat(bih.data(), bih.size()); pOut->init(dstfile.c_str()); ProgressDialog dlg((HWND)h, "Processing video stream", "Palettizing frames", (long)frames, true); vdblock<uint8> outbuf(rowbytes * pxsrc.h); const vdpixsize w = pxsrc.w; const vdpixsize h = pxsrc.h; try { for(uint32 frame=0; frame<frames; ++frame) { pVS->getFrame(frame); const uint8 *src = (const uint8 *)pxsrc.data; ptrdiff_t srcpitch = pxsrc.pitch; uint8 *dst = &outbuf[rowbytes * (pxsrc.h - 1)]; for(int y=0; y<h; ++y) { const uint8 *dr = ditherred[y & 15]; const uint8 *dg = dithergrn[y & 15]; const uint8 *db = ditherblu[y & 15]; for(int x=0; x<w; ++x) { const uint8 b = (uint8)((((src[0] * 1286)>>8) + dr[x&15]) >> 8); const uint8 g = (uint8)((((src[1] * 1543)>>8) + dg[x&15]) >> 8); const uint8 r = (uint8)((((src[2] * 1286)>>8) + db[x&15]) >> 8); src += 4; dst[x] = (uint8)(r*42 + g*6 + b); } vdptrstep(dst, -(ptrdiff_t)rowbytes); vdptrstep(src, srcpitch - w*4); } pVSOut->write(AVIOutputStream::kFlagKeyFrame, outbuf.data(), outbuf.size(), 1); dlg.advance(frame); dlg.check(); } } catch(const MyUserAbortError&) { } pVSOut->flush(); pOut->finalize(); } catch(const MyError& e) { e.post((HWND)h, g_szError); } }