uint64_t StringTable::addString (ustring str, ustring var_name)
{
    ASSERT (m_ptr && "StringTable has not been initialized");

    // The strings are laid out in the table as a struct:
    //
    //   struct TableRep {
    //       size_t len;
    //       size_t hash;
    //       char   str[len+1];
    //   };

    // Compute the size of the entry before adding it to the table 
    size_t size = sizeof(size_t) + sizeof(size_t) + str.size() + 1;
    if (((m_offset + size) >= m_size)) {
        reallocTable();
    }

    // It should be hard to trigger this assert, unless the table size is
    // very small and the string is very large.
    ASSERT (m_offset + size <= m_size && "String table allocation error");

    int offset = getOffset(str.string());
    if (offset < 0) {
        // Place the hash and length of the string before the characters
        size_t hash = str.hash();
        cudaMemcpy (m_ptr + m_offset, (void*)&hash, sizeof(size_t), cudaMemcpyHostToDevice);
        m_offset += sizeof(size_t);

        size_t len = str.length();
        cudaMemcpy (m_ptr + m_offset, (void*)&len, sizeof(size_t), cudaMemcpyHostToDevice);
        m_offset += sizeof(size_t);

        offset = m_offset;
        m_offset_map [str] = offset;
        m_name_map   [str] = var_name;

        // Copy the raw characters to the table
        cudaMemcpy (m_ptr + m_offset, str.c_str(), str.size() + 1, cudaMemcpyHostToDevice);
        m_offset += str.size() + 1;

        // Align the offset for the next entry to 8-byte boundaries
        m_offset = (m_offset + 0x7u) & ~0x7u;
    }

    uint64_t addr = reinterpret_cast<uint64_t>(m_ptr + offset);

    // Optionally create an OptiX variable for the string. It's not necessary to
    // create a variable for strings that do not appear by name in compiled code
    // (in either the OSL library functions or in the renderer).
    if (! var_name.empty()) {
        m_optix_ctx [var_name.string()]->setUserData (8, &addr);
    }

    return addr;
}
Example #2
0
ustring
b64_decode (const ustring &message)
{
   bool succeeded = false;
   auto_BIO mem    (BIO_new (BIO_s_mem ()));
   auto_BIO filter (BIO_new (BIO_f_base64()));
   ustring rv;

   BIO_set_flags(((BIO *) filter), BIO_FLAGS_BASE64_NO_NL);
   BIO_push  (filter, mem);

   if ((BIO_write (mem,
                   message.data (),
                   message.size ()) >= 0)
       &&
       (1 == BIO_flush (mem)))
   {
      const unsigned int Length = BIO_pending (filter);
      {
         unsigned char *tmp_buf = (unsigned char *) VHTI_alloc (Length);
         const int nread = BIO_read(filter, tmp_buf, Length);
         if (nread >= 0)
         {
            succeeded = true;
            rv.assign (tmp_buf, nread);
         }
         free (tmp_buf);
      }
   }

   if (!succeeded)
      throw SSL_ERROR;

   return rv;
}
Example #3
0
void CGlobalRecords::wide2str16(const ustring& str1, u16string& str2)
{
	size_t					resultSize, inbytesleft, outbytesleft;
	const uint8_t			*inbuf;
	iconv_t					cd;
	unsigned16_t			*outbuf, *origOutbuf = NULL;

	cd = iconv_open(UCS_2_INTERNAL, iconv_code.c_str());
	// user may have changed the conversion since the workbook was opened
	XL_ASSERT(!(cd == (iconv_t)(-1)));

	if(cd != (iconv_t)(-1)) {
		inbytesleft		= str1.size() * sizeof(unichar_t);
		outbytesleft	= str1.size() * sizeof(unsigned16_t);

		inbuf		= (uint8_t *)str1.data();
		origOutbuf	= (unsigned16_t *)calloc(outbytesleft, 1);
		outbuf		= origOutbuf;

		resultSize = iconv(cd, (char **)&inbuf, &inbytesleft, (char **)&outbuf, &outbytesleft);
		iconv_close(cd);
	} else {
		resultSize = (size_t)-1;
	}

	if(resultSize == (size_t)-1) {
		str2 = convFail;
	} else {
		str2.assign(origOutbuf, (size_t)(outbuf - origOutbuf));
	}
	free((void *)origOutbuf);
}
Example #4
0
ustring PubAddr::getPubOfPriv(ustring priv)
{
	OID CURVE = secp256k1();
	ECIES < ECP >::PrivateKey privK;

	Integer x;
	x.Decode(priv.c_str(), priv.size());
	privK.Initialize(CURVE, x);

	ECIES<ECP>::PublicKey pub;
	privK.MakePublicKey(pub);

	string encoded;
	int len = pub.GetPublicElement().x.MinEncodedSize();
	pub.GetPublicElement().x.Encode(StringSink(encoded).Ref(), len);

	len = pub.GetPublicElement().y.MinEncodedSize();
	pub.GetPublicElement().y.Encode(StringSink(encoded).Ref(), len);

	ustring ret;
	ret += 0x04;
	ret.fromString(encoded);

	return ret;
}
TEST(ustring_test, utility) {
  for (int i=0;i<(int)hiragana.size();++i) {
    EXPECT_TRUE(is_hiragana(hiragana[i]));
    EXPECT_FALSE(is_katakana(hiragana[i]));
    EXPECT_FALSE(is_kanji(hiragana[i]));
  }
  for (int i=0;i<(int)katakana.size();++i) {
    EXPECT_FALSE(is_hiragana(katakana[i]));
    EXPECT_TRUE(is_katakana(katakana[i]));
    EXPECT_FALSE(is_kanji(katakana[i]));
  }
  for (int i=0;i<(int)kanji.size();++i) {
    EXPECT_FALSE(is_hiragana(kanji[i]));
    EXPECT_FALSE(is_katakana(kanji[i]));
    EXPECT_TRUE(is_kanji(kanji[i]));
  }
}
Example #6
0
ustring basic_latin_to_zenkaku_latin(const ustring& us)
{
  ustring res=us;
  for (size_t i=0;i<us.size();++i) {
    res[i]=basic_latin_to_zenkaku_latin_impl(us[i]);
  }
  return res;
}
Example #7
0
ReadNormalizer::ReadNormalizer()
    : normalize_map() {
  const ustring unnormalized = string_to_ustring(
      "ガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポァィゥェォャュョッ");
  const ustring normalized = string_to_ustring(
      "カキクケコサシスセソタチツテトハヒフヘホハヒフヘホアイウエオヤユヤツ");
  for (size_t i = 0; i < unnormalized.size(); ++i) {
    normalize_map[unnormalized[i]] = normalized[i];
  }
}
Example #8
0
  ustring replace(ustring source, ustring fromStr, ustring toStr, int offset, int times)
  {
    int total = 0;
    ustring::size_type pos=offset;
    while ( ( (pos = source.find(fromStr, pos)) < Glib::ustring::npos) && ( (times==0) || (total++<times) ) )
      {
	source.replace(pos, fromStr.length(), toStr);
	pos+=toStr.size();
      }
    return source;
  }
Example #9
0
	Variant::Variant(const ustring& val) :
		Variant()
	{
		bstrVal = ::SysAllocStringLen(val.c_str(), val.size());
		if (bstrVal == nullptr) {
			vt = VT_ERROR;
			CheckCom(E_OUTOFMEMORY);
		} else {
			vt = VT_BSTR;
		}
	}
Example #10
0
float CFont::GetLineWidth(const ustring& text, size_t start /*= 0*/)
{
    size_t count = text.length()-start;
    for (int i = 0; i+start < text.size(); ++i)
    {
        if (text[i+start] == '\n')
        {
            count = i;
            break;
        }
    }
    return GetStringWidth(text.substr(start, count));
}
Example #11
0
void set_lyrics(DB_playItem_t *track, ustring lyrics) {
    signal_idle().connect_once([track, lyrics = move(lyrics)]() -> void {
        const char *artist, *title;
        {
            pl_lock_guard guard;

            if (!is_playing(track))
                return;
            artist = deadbeef->pl_find_meta(track, "artist");
            title  = deadbeef->pl_find_meta(track, "title");
        }
	if (!artist)
	    artist = _("Unknown Artist");
	if (!title)
	    title = _("Unknown Title");
        refBuffer->erase(refBuffer->begin(), refBuffer->end());
        refBuffer->insert_with_tags(refBuffer->begin(), title, tagsTitle);
        refBuffer->insert_with_tags(refBuffer->end(), ustring("\n") + artist + "\n\n", tagsArtist);

        bool italic = false;
        bool bold = false;
        size_t prev_mark = 0;
        vector<RefPtr<TextTag>> tags;
        while (prev_mark != ustring::npos) {
            size_t italic_mark = lyrics.find("''", prev_mark);
            if (italic_mark == ustring::npos)
                break;
            size_t bold_mark = ustring::npos;
            if (italic_mark < lyrics.size() - 2 && lyrics[italic_mark + 2] == '\'')
                bold_mark = italic_mark;

            tags.clear();
            if (italic) tags.push_back(tagItalic);
            if (bold)   tags.push_back(tagBold);
            refBuffer->insert_with_tags(refBuffer->end(),
                    lyrics.substr(prev_mark, min(bold_mark, italic_mark) - prev_mark), tags);

            if (bold_mark == ustring::npos) {
                prev_mark = italic_mark + 2;
                italic = !italic;
            } else {
                prev_mark = bold_mark + 3;
                bold = !bold;
            }
        }
        refBuffer->insert(refBuffer->end(), lyrics.substr(prev_mark)); // in case if no formatting found
        last = track;
    });
}
Example #12
0
bool PluginDisplay::search_equal(const Glib::RefPtr<Gtk::TreeModel>& model, int column, const ustring& key, const Gtk::TreeIter& iter) {
    PluginDesc *p = iter->get_value(plugin_liststore->col.pdesc);
    ustring txt(gx_system::to_string(p->UniqueID));
    if (txt.compare(0, ustring::npos, key, 0, key.size()) == 0) {
        return false;
    }
    txt = iter->get_value(plugin_liststore->col.name).lowercase();
    std::vector<ustring> keyvec;
    split(keyvec, key);
    for (std::vector<ustring>::iterator i = keyvec.begin(); i != keyvec.end(); ++i) {
        if (txt.find(*i) == ustring::npos) {
            return true;
	}
    }
    return false;
}
Example #13
0
ustring hankaku_to_zenkaku(const ustring& ustr){
  ustring res;
  for (size_t i=0;i<ustr.size();++i) {
    uchar c=res.size()>0?res[res.size()-1]:0xFFFFFFFFU;
    if (is_hankaku(ustr[i])) {
      if (ustr[i]==UTF_HANKAKU_DAKUTEN) { 
        if (c!=0xFFFFFFFFU&&is_katakana(c)&&take_dakuten_tbl[c-0x30a0]) {
          if (c==0x30a6) { // ウ
            res[res.size()-1]=0x30f4;
          } else if (c==0x30f2) { // ヲ
            res[res.size()-1]=0x30fa;
          } else {
            ++res[res.size()-1];
          }
        } else {
          res.push_back(UTF_ZENKAKU_DAKUTEN); // zenkaku dakuten
        }
      } else if (ustr[i]==UTF_HANKAKU_HANDAKUTEN) {
        if (c!=0xFFFFFFFFU&&is_katakana(c)&&take_handakuten_tbl[c-0x30a0]) {
          res[res.size()-1]+=2;
        } else {
          res.push_back(UTF_ZENKAKU_HANDAKUTEN); // zenkaku dakuten
        }
      } else {
        res.push_back(hankaku_zenkaku_tbl[ustr[i]-0xff60]);
      }
    } else if (ustr[i]==UTF_HANKAKU_BAR) { // -
      if (c!=0xFFFFFFFFU&&!is_basic_latin(c)) {
        res.push_back(UTF_ZENKAKU_BAR);
      } else {
        res.push_back(ustr[i]);
      }
    } else {
      res.push_back(ustr[i]);
    }
  } 
  return res;
}
Example #14
0
bool IConvert::convert(const ustring& from,string& to) {
    assert(!from.empty());
	size_t insize = 0;
	size_t outsize = 0;

    iconv(m_iconv_from_unicode, NULL, &insize, NULL, &outsize);

	insize = from.size() * sizeof(uchar_t);
	outsize = BUFFER_SIZE;

    size_t ret;
    char* psrc = (char*)from.data();
    char* pdest = m_buffer;

    ret = iconv(m_iconv_from_unicode, &psrc, &insize, &pdest, &outsize);

	if (ret == (size_t)(-1))
        return false;
    else {
        to.assign(m_buffer,pdest);
        //to.assign(m_buffer,(BUFFER_SIZE - outsize)/sizeof(char));
        return true;
    }
}
Example #15
0
ustring PubAddr::decode(ustring data, ustring privK)
{
	unsigned int p = 0;

	ustring IV = data.getUstring(16, p);
	unsigned int curveType = data.getInt16(p);
	unsigned int Xlen = data.getInt16(p);
	ustring X = data.getUstring(Xlen, p);
	unsigned int Ylen = data.getInt16(p);
	ustring Y = data.getUstring(Ylen, p);
	ustring cipherText = data.getUstring(data.size() - (p + 32), p);
	ustring MAC = data.getUstring(32, p);

	ustring pubK;
	pubK += 0x04;
	pubK += X;
	pubK += Y;


	OID CURVE = secp256k1();
	AutoSeededRandomPool rng;

	ECDH < ECP >::Domain dhA(CURVE), dhB(CURVE);
	SecByteBlock privA(privK.c_str(), dhA.PrivateKeyLength());
	SecByteBlock pubB(pubK.c_str(), dhB.PublicKeyLength());

	if (dhA.AgreedValueLength() != dhB.AgreedValueLength())
	{
		throw runtime_error("Shared shared size mismatch");
	}
	SecByteBlock sharedA(dhA.AgreedValueLength()), sharedB(dhB.AgreedValueLength());

	if (!dhA.Agree(sharedA, privA, pubB))
		throw runtime_error("Failed to reach shared secret (A)");


	Integer ssa, ssb;

	ssa.Decode(sharedA.BytePtr(), sharedA.SizeInBytes());

	uint8_t H[CryptoPP::SHA512::DIGESTSIZE];
	CryptoPP::SHA512 hash;

	hash.CalculateDigest(H, sharedA.BytePtr(), sharedA.SizeInBytes());



	AutoSeededRandomPool prng;

	byte key[32];

	memcpy(key, H, sizeof(key));

	byte Hkey[32];

	memcpy(Hkey, &(H[32]), sizeof(Hkey));

	byte iv[16];

	memcpy(iv, IV.c_str(), IV.size());

	string cipher = cipherText.toString(), encoded, recovered;

	string HMacPlain;

	p = 0;
	HMacPlain += data.getString(data.size()-32,p);

	string mac;

	try
	{
		HMAC<SHA256> hmac(Hkey, 32);
		StringSource s(HMacPlain, true,
			new HashFilter(hmac,
				new StringSink(mac)
				)
			);
	}
	catch (const CryptoPP::Exception& e)
	{
		throw runtime_error(e.what());
	}

	if (mac != MAC.toString())
		throw runtime_error("mac doesnt match");

	try
	{
		CBC_Mode< AES >::Decryption d;
		d.SetKeyWithIV(key, sizeof(key), iv);

		// The StreamTransformationFilter removes
		//  padding as required.
		StringSource s(cipher, true,
			new StreamTransformationFilter(d,
				new StringSink(recovered)
				) // StreamTransformationFilter
			); // StringSource
	}
	catch (const CryptoPP::Exception& e)
	{
		throw runtime_error(e.what());
	}

	ustring result;
	result.fromString(recovered);
	return result;
}
Example #16
0
		inline int set_string(ssize_t y, const ustring & str, PCWSTR eol) {
			EditorSetString ess = {(int)y, str.c_str(), eol, (int)str.size()};
			return	psi().EditorControl(ECTL_SETSTRING, &ess);
		}
TEST(ustring_test, zenkaku_latin_to_basic_latin_uchar) {
  ASSERT_EQ(basic_latin.size(), zenkaku_latin.size());
  for (size_t i=0;i<zenkaku_latin.size();++i) {
    EXPECT_EQ(basic_latin[i],zenkaku_latin_to_basic_latin(zenkaku_latin[i]));
  }
}
Example #18
0
INT_PTR editor_set_string(ssize_t y, ustring const& str, PCWSTR eol)
{
	return Far::Editor::set_string(y, str.c_str(), str.size(), eol);
}
Example #19
0
	BStr::BStr(const ustring & val)
	{
		m_str = ::SysAllocStringLen(val.c_str(), val.size());
		if (!m_str)
			CheckCom(E_OUTOFMEMORY);
	}
Example #20
0
    ustring processKeyUTF8 (ustring text, char key, InputMethodT im,
                            guint BackspaceChar) {
        // Case: Backspace character
        if (key == BackspaceChar) {
            _size_t_ pos = getLastPseudoWordPos (text);

            // Case: no word inside text
            if (pos == ustring::npos)
                return text.substr (0, text.size () - 1);

            ustring firstPart = text.substr (0, pos);
            ustring lastWord = text.substr (pos);
            Accents accent = getAccentFromWord (lastWord);
            lastWord.replace (lastWord.size () - 1, 1, "");
            return firstPart + addAccentToWord (lastWord, accent);
        }

        gchar lowCaseKey = _(key).lowercase ()[0];
        ustringArrayT availTrans = findTransform (__(lowCaseKey), im);
        availTrans = refineTransform (availTrans, text);

        // TODO refactor

        // Case: `key` is not for transforming
        if (availTrans.size () == 0) {
            ustring result = addChar (text, _(key));
            if (hasValidEndingConsonantsP (result)) {
                Accents accent = getAccentFromWord (result);
                result = removeAccentFromLastWord (result);
                result = addAccentToText (result, accent);
            }
            return result;
        }

        TransformTypeT markOrAccent;
        // cerr << "Text >> " << __(text) << " -> " << endl;
        ustring res = "";

        for (guint i = 0; i < availTrans.size (); i++) {
            const gchar *trans = availTrans[i].c_str ();
            TransformTypeT tmpMarkOrAccent = getTransformType (trans[2]);
            TransformT tmpTrans = getTransform (trans[2]);
            ustring tmpRes;

            if (tmpMarkOrAccent == TRANSFORM_MARK)
                tmpRes = addMarkToText (text, tmpTrans, trans[1]);
            else
                tmpRes = addAccentToText (text, tmpTrans);

            // cerr << " >> " << trans << " -> "
            //      << "mark? " << (tmpMarkOrAccent == TRANSFORM_MARK) << " -> "
            //      << "trans: " << tmpTrans << " -> "
            //      << __(tmpRes) << endl;

            if (res == "" || (tmpRes != text && res != "")) {
                res = tmpRes;
                markOrAccent = tmpMarkOrAccent;
            }
        }

        // Case: fallback to English
        if (res.size () == 0 || res == text) {
            if (markOrAccent == TRANSFORM_MARK)
                res = addChar (removeMarksFromLastWord (text), _(key));
            else
                res = addChar (removeAccentFromLastWord (text), _(key));
        }

        return res;
    }
Example #21
0
INT_PTR editor_set_string(ssize_t y, const ustring & str, PCWSTR eol)
{
	LogNoise(L"row: %5Id '%s'\n", y, str.c_str());
	return Far::Editor::set_string(y, str.c_str(), str.size(), eol);
}
Example #22
0
ustring PubAddr::buildAddressFromKeys(ustring Skey, ustring Ekey, int stream, int version)
{
	CryptoPP::SHA512 hash;
	byte digest[CryptoPP::SHA512::DIGESTSIZE];
	byte digest1[CryptoPP::SHA512::DIGESTSIZE];

	ustring buffer;
	if (Skey.size() == 64)
	{
		ustring tmp = Skey;
		Skey.clear();
		Skey += 0x04;
		Skey += tmp;
	}
	if (Ekey.size() == 64)
	{
		ustring tmp = Ekey;
		Ekey.clear();
		Ekey += 0x04;
		Ekey += tmp;
	}
	buffer += Skey;
	buffer += Ekey;

	hash.CalculateDigest(digest, (byte*)buffer.c_str(), buffer.length());

	CryptoPP::RIPEMD160 hash2;
	byte digest2[CryptoPP::RIPEMD160::DIGESTSIZE];
	hash2.CalculateDigest(digest2, digest, sizeof digest);

	ustring ripe;
	int i = 0;
	while (digest2[i] == 0x00)
		i++;

	while (i < sizeof digest2)
	{
		ripe += digest2[i];
		i++;
	}

	ustring tmp;
	tmp.appendVarInt_B(version);
	tmp.appendVarInt_B(stream);
	tmp += ripe;

	//generate checksum
	hash.CalculateDigest(digest, (byte*)tmp.c_str(), tmp.length());
	hash.CalculateDigest(digest1, digest, sizeof digest);

	tmp += digest1[0];
	tmp += digest1[1];
	tmp += digest1[2];
	tmp += digest1[3];

	//convert to base58
	char * ret = new char[256];
	size_t size = 256;
	if (!b58enc(ret, &size, tmp.c_str(), tmp.size()))
	{
		delete[] ret;
		throw runtime_error("cannot encode base58");
	}
	ustring addr;
	addr.fromString(string("BM-"));

	addr.append((unsigned char*)ret, size-1);
	delete[] ret;
	return addr;
}