using
System;
using
System.Xml;
namespace
TracealyzerCoder
{
class
Program
{
static
void
Main(
string
[] args)
{
Console.Write(
"input offline request file name: "
);
string
filename = Console.ReadLine();
try
{
XmlDocument requestXml =
new
XmlDocument();
requestXml.Load(filename);
RequestInfo info = XmlGetRequestInfo(requestXml);
GenerateActivationFile(info);
Console.WriteLine(
"OK"
);
}
catch
(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
static
RequestInfo XmlGetRequestInfo(XmlDocument xmlDocument)
{
string
[] nodeNames =
new
[] {
"Key"
,
"ProductId"
,
"Id"
,
"Name"
};
RequestInfo info =
new
RequestInfo();
foreach
(
string
name
in
nodeNames)
{
XmlNodeList nodes = xmlDocument.GetElementsByTagName(name);
if
(nodes.Count == 0)
{
throw
new
Exception(
"bad format of request file."
);
}
else
{
string
text = nodes[0].InnerText;
switch
(name)
{
case
"Key"
:
info.key = text;
break
;
case
"ProductId"
:
info.productId = text;
break
;
case
"Id"
:
info.id = text;
break
;
case
"Name"
:
info.name = text;
break
;
}
}
}
return
info;
}
static
void
GenerateActivationFile(RequestInfo info)
{
Random rd =
new
Random();
XmlDocument root =
new
XmlDocument();
XmlElement signedLicense = root.CreateElement(
"SignedLicense"
);
root.AppendChild(signedLicense);
XmlElement license = root.CreateElement(
"License"
);
signedLicense.AppendChild(license);
byte
[] haskkeyBytes =
new
byte
[20];
rd.NextBytes(haskkeyBytes);
XmlElement hashKey = root.CreateElement(
"HashedKey"
);
hashKey.InnerText = Convert.ToBase64String(haskkeyBytes);
license.AppendChild(hashKey);
XmlElement product = root.CreateElement(
"Product"
);
product.InnerText =
"Tracealyzer - Standard Edition"
;
license.AppendChild(product);
XmlElement productId = root.CreateElement(
"ProductId"
);
productId.InnerText = info.productId;
license.AppendChild(productId);
XmlElement editionId = root.CreateElement(
"EditionId"
);
editionId.InnerText =
"{050C587E-D34F-4361-B344-F8FCC4473477}"
;
license.AppendChild(editionId);
XmlElement licenseTo = root.CreateElement(
"LicensedTo"
);
licenseTo.InnerText = info.name;
license.AppendChild(licenseTo);
XmlElement expiresOn = root.CreateElement(
"ExpiresOn"
);
expiresOn.InnerText =
"Never"
;
license.AppendChild(expiresOn);
XmlElement supportThrough = root.CreateElement(
"SupportThrough"
);
supportThrough.InnerText =
"2099-12-31"
;
license.AppendChild(supportThrough);
license.AppendChild(XmlAddComponents(root));
XmlElement nodes = root.CreateElement(
"Nodes"
);
license.AppendChild(nodes);
XmlElement node = root.CreateElement(
"Node"
);
nodes.AppendChild(node);
XmlElement id = root.CreateElement(
"Id"
);
id.InnerText = info.id;
node.AppendChild(id);
XmlElement name = root.CreateElement(
"Name"
);
name.InnerText = info.name;
node.AppendChild(name);
byte
[] licenseBytes =
new
byte
[256];
rd.NextBytes(licenseBytes);
XmlElement signature = root.CreateElement(
"Signature"
);
signature.InnerText = Convert.ToBase64String(licenseBytes);
signedLicense.AppendChild(signature);
root.Save(
"result.xml"
);
}
static
XmlElement XmlAddComponents(XmlDocument root)
{
ComponentXml[] components =
new
ComponentXml[]
{
new
ComponentXml(
"{B1CF5B88-ADA5-4B2A-81AF-257054106205}"
,
"Tracealyzer Application"
),
new
ComponentXml(
"{469D108A-B824-4C27-99ED-98B8629BFCE0}"
,
"FreeRTOS Support"
),
new
ComponentXml(
"{95F4ED62-6F8E-4EE3-8ED2-1A6F68B63A50}"
,
"Keil RTX5 Support"
),
new
ComponentXml(
"{629259CF-458D-4F05-9545-99DA60198B26}"
,
"µC/OS-III Support"
)
};
XmlElement componentsXml = root.CreateElement(
"Components"
);
foreach
(ComponentXml component
in
components)
{
XmlElement componentXml = root.CreateElement(
"Component"
);
XmlElement idXml = root.CreateElement(
"Id"
);
idXml.InnerText = component.id;
XmlElement nameXml = root.CreateElement(
"Name"
);
nameXml.InnerText = component.name;
componentXml.AppendChild(idXml);
componentXml.AppendChild(nameXml);
componentsXml.AppendChild(componentXml);
}
return
componentsXml;
}
}
class
RequestInfo
{
public
string
key;
public
string
productId;
public
string
id;
public
string
name;
}
class
ComponentXml
{
public
string
id;
public
string
name;
public
ComponentXml(
string
_id,
string
_name)
{
this
.id = _id;
this
.name = _name;
}
}
}