Improved tax levels; upgrade to schema 3

This commit is contained in:
Rick Falkvinge
2014-10-21 07:54:50 +02:00
parent 8632645dc5
commit fb24670135
5 changed files with 22 additions and 123 deletions
-1
View File
@@ -65,7 +65,6 @@
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Console.cs" />
<Compile Include="DotNetChartingCrack.cs" />
<Content Include="App.ico" />
</ItemGroup>
<ItemGroup>
-111
View File
@@ -1,111 +0,0 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Swarmops
{
public class DotNetChartingCrack
{
static public string DecodeFile (string licenseFile)
{
string text = null;
FileStream stream = null;
try
{
string strPassword = null;
stream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read);
MemoryStream stream2 = new MemoryStream();
byte[] buffer = new byte[0x100];
long num = 0;
long length = stream.Length;
int count = 0;
for (int i = 0x80; i < 0x88; i++)
{
strPassword = strPassword + Convert.ToChar(i).ToString();
}
DES des = new DESCryptoServiceProvider();
byte[] bytes = new PasswordDeriveBytes(strPassword, null).GetBytes(8);
des.Key = bytes;
des.IV = new byte[8];
CryptoStream stream3 = new CryptoStream(stream2, des.CreateDecryptor(), CryptoStreamMode.Write);
while (num < length)
{
count = stream.Read(buffer, 0, 0x100);
stream3.Write(buffer, 0, count);
num += count;
}
stream3.Close();
string text3 = Encoding.ASCII.GetString(stream2.GetBuffer(), 0, count);
int index = text3.IndexOf('\0');
text = text3.Substring(0, index);
stream2.Close();
}
catch
{
}
finally
{
stream.Close();
}
return text;
}
static public void EncodeFile3x(string licenseFile, string domainToLicense)
{
// The format of the encrypted license file is "m[contractnumber]domain". However, the
// verifier only checks for the presence of the first lowercase "m", then finds the
// end square bracket and reads the domain from there onwards. So this works too.
WriteLicenseFile(licenseFile, "methinks you should Vote Pirate for a DRM free world.]" + domainToLicense);
}
static public void EncodeFile4x(string licenseFile, string domainToLicense)
{
// The format of the encrypted license file is "o[contractnumber]domain". However, the
// verifier only checks for the presence of the first lowercase "o", then finds the
// end square bracket and reads the domain from there onwards. So this works too.
WriteLicenseFile(licenseFile, "oh, was this the best you could do?]" + domainToLicense);
}
static public void WriteManyFiles (string path, string domainToLicense)
{
for (char ch = 'p'; ch <= 'z'; ch++)
{
WriteLicenseFile(Path.Combine(path, ch + "-" + domainToLicense + ".lic"), ch + "]" + domainToLicense);
}
}
static private void WriteLicenseFile(string licenseFile, string contents)
{
FileStream stream = null;
try
{
string strPassword = null;
stream = new FileStream(licenseFile, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[0x100];
for (int i = 0x80; i < 0x88; i++)
{
strPassword = strPassword + Convert.ToChar(i).ToString();
}
DES des = new DESCryptoServiceProvider();
byte[] bytes = new PasswordDeriveBytes(strPassword, null).GetBytes(8);
byte[] domainBytes = Encoding.Default.GetBytes(contents);
des.Key = bytes;
des.IV = new byte[8];
CryptoStream stream3 = new CryptoStream(stream, des.CreateEncryptor(), CryptoStreamMode.Write);
stream3.Write(domainBytes, 0, domainBytes.Length);
stream3.Close();
}
catch
{
}
finally
{
stream.Close();
}
}
}
}
+1 -1
View File
@@ -71,7 +71,7 @@ namespace Swarmops.Database
/// </summary>
public static int DbVersionExpected
{
get { return 2; }
get { return 3; }
}
+18 -5
View File
@@ -10,20 +10,32 @@ namespace Swarmops.Database
#region Record reading - SELECT statements
public double GetTaxLevel(int countryId, int taxLevelId, int grossSalary)
public double GetSalaryTaxLevel(int countryId, int taxLevelId, int grossSalary)
{
using (DbConnection connection = GetMySqlDbConnection())
{
int thisYear = DateTime.Today.Year; // TODO: Fiscal years may be different in some weird countries
// TODO: Adapt currency if the operating currency is different from the tax currency? Or should that be in the logic layer?
connection.Open();
DbCommand command =
GetDbCommand(
String.Format("SELECT Tax FROM SalaryTaxLevels WHERE CountryId={0} AND TaxLevelId={1} AND BracketLow<={2} ORDER BY BracketLow Desc LIMIT 1", countryId, taxLevelId, grossSalary), connection);
String.Format("SELECT Tax,Year FROM SalaryTaxLevels WHERE CountryId={0} AND TaxLevelId={1} AND BracketLow<={2} AND Year<={3} ORDER BY BracketLow Desc LIMIT 1", countryId, taxLevelId, grossSalary, thisYear), connection);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
int year = reader.GetInt32(1);
if (thisYear != year)
{
// TODO: Send warning somewhere that non-current data was used for tax deduction, which is usually ok for
// TODO: a very limited time, but not in a sustained situation
}
int taxLevel = reader.GetInt32(0);
if (taxLevel < 0)
@@ -47,19 +59,20 @@ namespace Swarmops.Database
#region Creation and manipulation - stored procedures
public void CreateTaxLevel (int countryId, int taxLevelId, int bracketLow, double tax)
public void CreateSalaryTaxLevel (int countryId, int taxLevelId, int bracketLow, int year, double tax)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("CreateTaxLevel", connection);
DbCommand command = GetDbCommand("CreateSalaryTaxLevel", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "countryId", countryId);
AddParameterWithName(command, "taxLevelId", taxLevelId);
AddParameterWithName(command, "bracketLow", bracketLow);
AddParameterWithName(command, "tax", tax < 1.0 ? (int) (-tax*100) : (int) tax);
AddParameterWithName(command, "year", year);
AddParameterWithName(command, "tax", tax < 1.0 ? (int)(-tax * 100) : (int)tax);
command.ExecuteNonQuery();
}
+3 -5
View File
@@ -14,7 +14,7 @@ namespace Swarmops.Logic.Financial
return 0.0; // no tax
}
double taxLevel = SwarmDb.GetDatabaseForReading().GetTaxLevel(country.Identity, taxLevelIdentifier,
double taxLevel = SwarmDb.GetDatabaseForReading().GetSalaryTaxLevel(country.Identity, taxLevelIdentifier,
(int) Math.Floor(grossSalary));
if (taxLevel < 1.0)
@@ -35,9 +35,7 @@ namespace Swarmops.Logic.Financial
Persistence.Key["TaxDataImporting"] = "1";
// Clear existing tables for countryId
SwarmDb.GetDatabaseForWriting().DeleteTaxLevels(country.Identity);
int year = DateTime.Today.Year;
string[] lines = data.Split('\n'); // if \r\n is used, the \r will be harmlessly at the end of lines
@@ -68,7 +66,7 @@ namespace Swarmops.Logic.Financial
tax = tax / 100.0;
}
SwarmDb.GetDatabaseForWriting().CreateTaxLevel(country.Identity, taxLevelId, lowerBracket, tax);
SwarmDb.GetDatabaseForWriting().CreateSalaryTaxLevel(country.Identity, taxLevelId, lowerBracket, year, tax);
}
}