Пример #1
0
void ByteArray::writeUTF(const tiny_string& str)
{
	getBuffer(position+str.numBytes()+2,true);
	if(str.numBytes() > 65535)
	{
		throwError<RangeError>(kParamRangeError);
	}
	uint16_t numBytes=endianIn((uint16_t)str.numBytes());
	memcpy(bytes+position,&numBytes,2);
	memcpy(bytes+position+2,str.raw_buf(),str.numBytes());
	position+=str.numBytes()+2;
}
Пример #2
0
/* like strcasecmp(s1.raw_buf(),s2.raw_buf()) but for unicode
 * TODO: slow! */
int tiny_string::strcasecmp(tiny_string& s2) const
{
	char* str1 = g_utf8_casefold(this->raw_buf(),this->numBytes());
	char* str2 = g_utf8_casefold(s2.raw_buf(),s2.numBytes());
	int ret = g_utf8_collate(str1,str2);
	g_free(str1);
	g_free(str2);
	return ret;
}
Пример #3
0
/* start is an index of characters.
 * returns index of character */
uint32_t tiny_string::find(const tiny_string& needle, uint32_t start) const
{
	//TODO: omit copy into std::string
	size_t bytestart = g_utf8_offset_to_pointer(buf,start) - buf;
	size_t bytepos = std::string(*this).find(needle.raw_buf(),bytestart,needle.numBytes());
	if(bytepos == std::string::npos)
		return npos;
	else
		return g_utf8_pointer_to_offset(buf,buf+bytepos);
}
Пример #4
0
tiny_string Vector::toJSON(std::vector<ASObject *> &path, IFunction *replacer, const tiny_string &spaces, const tiny_string &filter)
{
	bool ok;
	tiny_string res = call_toJSON(ok,path,replacer,spaces,filter);
	if (ok)
		return res;
	// check for cylic reference
	if (std::find(path.begin(),path.end(), this) != path.end())
		throwError<TypeError>(kJSONCyclicStructure);

	path.push_back(this);
	res += "[";
	bool bfirst = true;
	tiny_string newline = (spaces.empty() ? "" : "\n");
	for (unsigned int i =0;  i < vec.size(); i++)
	{
		tiny_string subres;
		ASObject* o = vec[i];
		if (!o)
			o = getSystemState()->getNullRef();
		if (replacer != NULL)
		{
			ASObject* params[2];
			
			params[0] = abstract_di(getSystemState(),i);
			params[0]->incRef();
			params[1] = o;
			params[1]->incRef();
			ASObject *funcret=replacer->call(getSystemState()->getNullRef(), params, 2);
			if (funcret)
				subres = funcret->toJSON(path,NULL,spaces,filter);
		}
		else
		{
			subres = o->toJSON(path,replacer,spaces,filter);
		}
		if (!subres.empty())
		{
			if (!bfirst)
				res += ",";
			res += newline+spaces;
			
			bfirst = false;
			res += subres;
		}
	}
	if (!bfirst)
		res += newline+spaces.substr_bytes(0,spaces.numBytes()/2);
	res += "]";
	path.pop_back();
	return res;
}
Пример #5
0
int JSON::parseNumber(const tiny_string &jsonstring, int pos, ASObject** parent, const multiname& key)
{
	int len = jsonstring.numBytes();
	tiny_string res;
	bool done = false;
	while (!done && pos < len)
	{
		char c = jsonstring.charAt(pos);
		switch(c)
		{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '-':
			case '+':
			case '.':
			case 'E':
			case 'e':
				res += c;
				pos++;
				break;
			default:
				done = true;
				break;
		}
	}
	ASString* numstr = Class<ASString>::getInstanceS(res);
	number_t num = numstr->toNumber();

	if (std::isnan(num))
		throwError<SyntaxError>(kJSONInvalidParseInput);

	if (*parent == NULL)
		*parent = Class<Number>::getInstanceS(num);
	else 
	{
		(*parent)->setVariableByMultiname(key,Class<Number>::getInstanceS(num),ASObject::CONST_NOT_ALLOWED);
	}
	return pos;
}
Пример #6
0
void JSON::parseAll(const tiny_string &jsonstring, ASObject** parent , const multiname& key, IFunction *reviver)
{
	int len = jsonstring.numBytes();
	int pos = 0;
	while (pos < len)
	{
		if (*parent && (*parent)->isPrimitive())
			throwError<SyntaxError>(kJSONInvalidParseInput);
		pos = parse(jsonstring, pos, parent , key, reviver);
		while (jsonstring.charAt(pos) == ' ' ||
			   jsonstring.charAt(pos) == '\t' ||
			   jsonstring.charAt(pos) == '\n' ||
			   jsonstring.charAt(pos) == '\r'
			   )
			pos++;
	}
}
Пример #7
0
void XMLSocket::connect(tiny_string host, int port)
{
	if (port <= 0 || port > 65535)
		throw Class<SecurityError>::getInstanceS(getSystemState(),"Invalid port");

	if (host.empty())
		host = getSys()->mainClip->getOrigin().getHostname();

	if (isConnected())
		throw Class<IOError>::getInstanceS(getSystemState(),"Already connected");

	// Host shouldn't contain scheme or port
	if (host.strchr(':') != NULL)
		throw Class<SecurityError>::getInstanceS(getSystemState(),"Invalid hostname");

	// Check sandbox and policy file
	size_t buflen = host.numBytes() + 22;
	char *urlbuf = g_newa(char, buflen);
	snprintf(urlbuf, buflen, "xmlsocket://%s:%d", host.raw_buf(), port);
	URLInfo url(urlbuf);

	getSystemState()->securityManager->checkURLStaticAndThrow(url,
		~(SecurityManager::LOCAL_WITH_FILE),
		SecurityManager::LOCAL_WITH_FILE | SecurityManager::LOCAL_TRUSTED,
		true);

	SecurityManager::EVALUATIONRESULT evaluationResult;
	evaluationResult = getSys()->securityManager->evaluateSocketConnection(url, true);
	if(evaluationResult != SecurityManager::ALLOWED)
	{
		incRef();
		getVm(getSystemState())->addEvent(_MR(this), _MR(Class<SecurityErrorEvent>::getInstanceS(getSystemState(),"No policy file allows socket connection")));
		return;
	}

	incRef();
	XMLSocketThread *thread = new XMLSocketThread(_MR(this), host, port, timeout);
	getSys()->addJob(thread);
	job = thread;
}
Пример #8
0
int JSON::parseNull(const tiny_string &jsonstring, int pos,ASObject** parent,const multiname& key)
{
	int len = jsonstring.numBytes();
	if (len >= pos+4)
	{
		if (jsonstring.charAt(pos) == 'n' && 
				jsonstring.charAt(pos + 1) == 'u' &&
				jsonstring.charAt(pos + 2) == 'l' && 
				jsonstring.charAt(pos + 3) == 'l')
		{
			pos += 4;
			if (*parent == NULL)
				*parent = getSys()->getNullRef();
			else 
				(*parent)->setVariableByMultiname(key,getSys()->getNullRef(),ASObject::CONST_NOT_ALLOWED);
		}
		else
			throwError<SyntaxError>(kJSONInvalidParseInput);
	}
	else
		throwError<SyntaxError>(kJSONInvalidParseInput);
	return pos;
}
Пример #9
0
int JSON::parseTrue(const tiny_string &jsonstring, int pos,ASObject** parent,const multiname& key)
{
	int len = jsonstring.numBytes();
	if (len >= pos+4)
	{
		if (jsonstring.charAt(pos) == 't' && 
				jsonstring.charAt(pos + 1) == 'r' &&
				jsonstring.charAt(pos + 2) == 'u' && 
				jsonstring.charAt(pos + 3) == 'e')
		{
			pos += 4;
			if (*parent == NULL)
				*parent = abstract_b(true);
			else 
				(*parent)->setVariableByMultiname(key,abstract_b(true),ASObject::CONST_NOT_ALLOWED);
		}
		else
			throwError<SyntaxError>(kJSONInvalidParseInput);
	}
	else
		throwError<SyntaxError>(kJSONInvalidParseInput);
	return pos;
}
Пример #10
0
int JSON::parse(const tiny_string &jsonstring, int pos, ASObject** parent , const multiname& key, IFunction *reviver)
{
	while (jsonstring.charAt(pos) == ' ' ||
		   jsonstring.charAt(pos) == '\t' ||
		   jsonstring.charAt(pos) == '\n' ||
		   jsonstring.charAt(pos) == '\r'
		   )
		   pos++;
	int len = jsonstring.numBytes();
	if (pos < len)
	{
		char c = jsonstring.charAt(pos);
		switch(c)
		{
			case '{':
				pos = parseObject(jsonstring,pos,parent,key, reviver);
				break;
			case '[': 
				pos = parseArray(jsonstring,pos,parent,key, reviver);
				break;
			case '"':
				pos = parseString(jsonstring,pos,parent,key);
				break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '-':
				pos = parseNumber(jsonstring,pos,parent,key);
				break;
			case 't':
				pos = parseTrue(jsonstring,pos,parent,key);
				break;
			case 'f':
				pos = parseFalse(jsonstring,pos,parent,key);
				break;
			case 'n':
				pos = parseNull(jsonstring,pos,parent,key);
				break;
			default:
				throwError<SyntaxError>(kJSONInvalidParseInput);
		}
	}
	if (reviver)
	{
		bool haskey = key.name_type!= multiname::NAME_OBJECT;
		ASObject* params[2];
		
		if (haskey)
		{
			params[0] = Class<ASString>::getInstanceS(key.normalizedName());
			if ((*parent)->hasPropertyByMultiname(key,true,false))
			{
				params[1] = (*parent)->getVariableByMultiname(key).getPtr();
				params[1]->incRef();
			}
			else
				params[1] = getSys()->getNullRef();
		}
		else
		{
			params[0] = Class<ASString>::getInstanceS("");
			params[1] = *parent;
			params[1]->incRef();
		}

		ASObject *funcret=reviver->call(getSys()->getNullRef(), params, 2);
		if(funcret)
		{
			if (haskey)
			{
				if (funcret->is<Undefined>())
				{
					(*parent)->deleteVariableByMultiname(key);
					funcret->decRef();
				}
				else
				{
					(*parent)->setVariableByMultiname(key,funcret,ASObject::CONST_NOT_ALLOWED);
				}
			}
			else 
				*parent= funcret;
		}
	}
	return pos;
}