Beispiel #1
0
void cSHA1Checksum::DigestToJava(const Checksum & a_Digest, AString & a_Out)
{
	Checksum Digest;
	memcpy(Digest, a_Digest, sizeof(Digest));
	
	bool IsNegative = (Digest[0] >= 0x80);
	if (IsNegative)
	{
		// Two's complement:
		bool carry = true;  // Add one to the whole number
		for (int i = 19; i >= 0; i--)
		{
			Digest[i] = ~Digest[i];
			if (carry)
			{
				carry = (Digest[i] == 0xff);
				Digest[i]++;
			}
		}
	}
	a_Out.clear();
	a_Out.reserve(40);
	for (int i = 0; i < 20; i++)
	{
		AppendPrintf(a_Out, "%02x", Digest[i]);
	}
	while ((a_Out.length() > 0) && (a_Out[0] == '0'))
	{
		a_Out.erase(0, 1);
	}
	if (IsNegative)
	{
		a_Out.insert(0, "-");
	}
}
Beispiel #2
0
AString cMojangAPI::MakeUUIDShort(const AString & a_UUID)
{
	// Note: we only check the string's length, not the actual content
	switch (a_UUID.size())
	{
		case 32:
		{
			// Already is a short UUID, only lowercase
			return StrToLower(a_UUID);
		}
		
		case 36:
		{
			// Remove the dashes from the string by appending together the parts between them:
			AString res;
			res.reserve(32);
			res.append(a_UUID, 0, 8);
			res.append(a_UUID, 9, 4);
			res.append(a_UUID, 14, 4);
			res.append(a_UUID, 19, 4);
			res.append(a_UUID, 24, 12);
			return StrToLower(res);
		}
	}
	LOGWARNING("%s: Not an UUID: \"%s\".", __FUNCTION__, a_UUID.c_str());
	return "";
}
Beispiel #3
0
AString cMojangAPI::MakeUUIDDashed(const AString & a_UUID)
{
	// Note: we only check the string's length, not the actual content
	switch (a_UUID.size())
	{
		case 36:
		{
			// Already is a dashed UUID, only lowercase
			return StrToLower(a_UUID);
		}
		
		case 32:
		{
			// Insert dashes at the proper positions:
			AString res;
			res.reserve(36);
			res.append(a_UUID, 0, 8);
			res.push_back('-');
			res.append(a_UUID, 8, 4);
			res.push_back('-');
			res.append(a_UUID, 12, 4);
			res.push_back('-');
			res.append(a_UUID, 16, 4);
			res.push_back('-');
			res.append(a_UUID, 20, 12);
			return StrToLower(res);
		}
	}
	LOGWARNING("%s: Not an UUID: \"%s\".", __FUNCTION__, a_UUID.c_str());
	return "";
}
Beispiel #4
0
bool cByteBuffer::ReadString(AString & a_String, int a_Count)
{
	CHECK_THREAD;
	CheckValid();
	ASSERT(a_Count >= 0);
	NEEDBYTES(a_Count);
	a_String.clear();
	a_String.reserve(a_Count);
	int BytesToEndOfBuffer = m_BufferSize - m_ReadPos;
	ASSERT(BytesToEndOfBuffer >= 0);  // Sanity check
	if (BytesToEndOfBuffer <= a_Count)
	{
		// Reading across the ringbuffer end, read the first part and adjust parameters:
		if (BytesToEndOfBuffer > 0)
		{
			a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer);
			a_Count -= BytesToEndOfBuffer;
		}
		m_ReadPos = 0;
	}

	// Read the rest of the bytes in a single read (guaranteed to fit):
	if (a_Count > 0)
	{
		a_String.append(m_Buffer + m_ReadPos, a_Count);
		m_ReadPos += a_Count;
	}
	return true;
}
Beispiel #5
0
// Converts a raw 160-bit SHA1 digest into a Java Hex representation
// According to http://wiki.vg/wiki/index.php?title=Protocol_Encryption&oldid=2802
static void DigestToJava(byte a_Digest[20], AString & a_Out)
{
	bool IsNegative = (a_Digest[0] >= 0x80);
	if (IsNegative)
	{
		// Two's complement:
		bool carry = true;  // Add one to the whole number
		for (int i = 19; i >= 0; i--)
		{
			a_Digest[i] = ~a_Digest[i];
			if (carry)
			{
				carry = (a_Digest[i] == 0xff);
				a_Digest[i]++;
			}
		}
	}
	a_Out.clear();
	a_Out.reserve(40);
	for (int i = 0; i < 20; i++)
	{
		AppendPrintf(a_Out, "%02x", a_Digest[i]);
	}
	while ((a_Out.length() > 0) && (a_Out[0] == '0'))
	{
		a_Out.erase(0, 1);
	}
	if (IsNegative)
	{
		a_Out.insert(0, "-");
	}
}
Beispiel #6
0
AString cWebAdmin::GetHTMLEscapedString(const AString & a_Input)
{
	AString dst;
	dst.reserve(a_Input.length());

	// Loop over input and substitute HTML characters for their alternatives:
	size_t len = a_Input.length();
	for (size_t i = 0; i < len; i++)
	{
		switch (a_Input[i])
		{
			case '&':  dst.append("&amp;");  break;
			case '\'': dst.append("&apos;"); break;
			case '"':  dst.append("&quot;"); break;
			case '<':  dst.append("&lt;");   break;
			case '>':  dst.append("&gt;");   break;
			default:
			{
				dst.push_back(a_Input[i]);
				break;
			}
		}  // switch (a_Input[i])
	}  // for i - a_Input[]

	return dst;
}
Beispiel #7
0
int CompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Compressed)
{
	// Compress a_Data into a_Compressed using GZIP; return Z_XXX error constants same as zlib's compress2()

	a_Compressed.reserve(a_Length);

	char Buffer[64 KiB];
	z_stream strm;
	memset(&strm, 0, sizeof(strm));
	strm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(a_Data));
	strm.avail_in = static_cast<uInt>(a_Length);
	strm.next_out = reinterpret_cast<Bytef *>(Buffer);
	strm.avail_out = sizeof(Buffer);
	
	int res = deflateInit2(&strm, 9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY);
	if (res != Z_OK)
	{
		LOG("%s: compression initialization failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
		return res;
	}
	
	for (;;)
	{
		res = deflate(&strm, Z_FINISH);
		switch (res)
		{
			case Z_OK:
			{
				// Some data has been compressed. Consume the buffer and continue compressing
				a_Compressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
				strm.next_out = reinterpret_cast<Bytef *>(Buffer);
				strm.avail_out = sizeof(Buffer);
				if (strm.avail_in == 0)
				{
					// All data has been compressed
					deflateEnd(&strm);
					return Z_OK;
				}
				break;
			}
			
			case Z_STREAM_END:
			{
				// Finished compressing. Consume the rest of the buffer and return
				a_Compressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
				deflateEnd(&strm);
				return Z_OK;
			}
			
			default:
			{
				// An error has occurred, log it and return the error value
				LOG("%s: compression failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
				deflateEnd(&strm);
				return res;
			}
		}  // switch (res)
	}  // while (true)
}
Beispiel #8
0
extern int InflateString(const char * a_Data, size_t a_Length, AString & a_Uncompressed)
{
	a_Uncompressed.reserve(a_Length);

	char Buffer[64 KiB];
	z_stream strm;
	memset(&strm, 0, sizeof(strm));
	strm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(a_Data));
	strm.avail_in = static_cast<uInt>(a_Length);
	strm.next_out = reinterpret_cast<Bytef *>(Buffer);
	strm.avail_out = sizeof(Buffer);
	
	int res = inflateInit(&strm);  // Force GZIP decoding
	if (res != Z_OK)
	{
		LOG("%s: inflation initialization failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
		return res;
	}
	
	for (;;)
	{
		res = inflate(&strm, Z_NO_FLUSH);
		switch (res)
		{
			case Z_OK:
			{
				// Some data has been uncompressed. Consume the buffer and continue uncompressing
				a_Uncompressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
				strm.next_out = reinterpret_cast<Bytef *>(Buffer);
				strm.avail_out = sizeof(Buffer);
				if (strm.avail_in == 0)
				{
					// All data has been uncompressed
					inflateEnd(&strm);
					return Z_OK;
				}
				break;
			}
			
			case Z_STREAM_END:
			{
				// Finished uncompressing. Consume the rest of the buffer and return
				a_Uncompressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
				inflateEnd(&strm);
				return Z_OK;
			}
			
			default:
			{
				// An error has occurred, log it and return the error value
				LOG("%s: inflation failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
				inflateEnd(&strm);
				return res;
			}
		}  // switch (res)
	}  // while (true)
}
Beispiel #9
0
AString cCaveTunnel::ExportAsSVG(int a_Color, int a_OffsetX, int a_OffsetZ) const
{
	AString SVG;
	SVG.reserve(m_Points.size() * 20 + 200);
	AppendPrintf(SVG, "<path style=\"fill:none;stroke:#%06x;stroke-width:1px;\"\nd=\"", a_Color);
	char Prefix = 'M';  // The first point needs "M" prefix, all the others need "L"
	for (cCaveDefPoints::const_iterator itr = m_Points.begin(); itr != m_Points.end(); ++itr)
	{
		AppendPrintf(SVG, "%c %d, %d ", Prefix, a_OffsetX + itr->m_BlockX, a_OffsetZ + itr->m_BlockZ);
		Prefix = 'L';
	}
	SVG.append("\"/>\n");
	return SVG;
}
Beispiel #10
0
AString cWebPlugin::SafeString(const AString & a_String)
{
	AString RetVal;
	auto len = a_String.size();
	RetVal.reserve(len);
	for (size_t i = 0; i < len; ++i)
	{
		char c = a_String[i];
		if (c == ' ')
		{
			c = '_';
		}
		RetVal.push_back(c);
	}
	return RetVal;
}
Beispiel #11
0
void cSpringStatsFactory::SaveStatistics(const cSpringStats::cStats::SpringStats & a_Stats, const AString & a_FileName)
{
	cFile f(a_FileName, cFile::fmWrite);
	if (!f.IsOpen())
	{
		LOG("Cannot open file \"%s\" for writing!", a_FileName.c_str());
		return;
	}
	for (int Height = 0; Height < 256; Height++)
	{
		AString Line;
		Line.reserve(2000);
		Printf(Line, "%d\t", Height);
		for (int Biome = 0; Biome < 256; Biome++)
		{
			AppendPrintf(Line, "%llu\t", a_Stats[Height][Biome]);
		}
		Line.append("\n");
		f.Write(Line.c_str(), Line.size());
	}
}
Beispiel #12
0
AString cStructGenWormNestCaves::cCaveSystem::ExportAsSVG(int a_Color, int a_OffsetX, int a_OffsetZ) const
{
	AString SVG;
	SVG.reserve(512 * 1024);
	for (cCaveTunnels::const_iterator itr = m_Tunnels.begin(), end = m_Tunnels.end(); itr != end; ++itr)
	{
		SVG.append((*itr)->ExportAsSVG(a_Color, a_OffsetX, a_OffsetZ));
	}  // for itr - m_Tunnels[]

	// Base point highlight:
	AppendPrintf(SVG, "<path style=\"fill:none;stroke:#ff0000;stroke-width:1px;\"\nd=\"M %d,%d L %d,%d\"/>\n",
		a_OffsetX + m_BlockX - 5, a_OffsetZ + m_BlockZ, a_OffsetX + m_BlockX + 5, a_OffsetZ + m_BlockZ
	);
	AppendPrintf(SVG, "<path style=\"fill:none;stroke:#ff0000;stroke-width:1px;\"\nd=\"M %d,%d L %d,%d\"/>\n",
		a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ - 5, a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ + 5
	);
	
	// A gray line from the base point to the first point of the ravine, for identification:
	AppendPrintf(SVG, "<path style=\"fill:none;stroke:#cfcfcf;stroke-width:1px;\"\nd=\"M %d,%d L %d,%d\"/>\n",
		a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ, 
		a_OffsetX + m_Tunnels.front()->m_Points.front().m_BlockX, 
		a_OffsetZ + m_Tunnels.front()->m_Points.front().m_BlockZ
	);
	
	// Offset guides:
	if (a_OffsetX > 0)
	{
		AppendPrintf(SVG, "<path style=\"fill:none;stroke:#0000ff;stroke-width:1px;\"\nd=\"M %d,0 L %d,1024\"/>\n",
			a_OffsetX, a_OffsetX
		);
	}
	if (a_OffsetZ > 0)
	{
		AppendPrintf(SVG, "<path style=\"fill:none;stroke:#0000ff;stroke-width:1px;\"\nd=\"M 0,%d L 1024,%d\"/>\n",
			a_OffsetZ, a_OffsetZ
		);
	}
	
	return SVG;
}