Beruflich Dokumente
Kultur Dokumente
js
// Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
// This work is free. You can redistribute it and/or modify it
// under the terms of the WTFPL, Version 2
// For more information see LICENSE.txt or http://www.wtfpl.net/
//
// For more information, the home page:
// http://pieroxy.net/blog/pages/lz-string/testing.html
//
// LZ-based compression algorithm, version 1.4.3
var LZString = (function() {
// private property
var f = String.fromCharCode;
var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
var baseReverseDic = {};
var LZString = {
compressToBase64 : function (input) {
if (input == null) return "";
var res = LZString._compress(input, 6, function(a){return keyStrBase64.charAt(a);});
switch (res.length % 4) { // To produce valid Base64
default: // When could this happen ?
case 0 : return res;
case 1 : return res+"===";
case 2 : return res+"==";
case 3 : return res+"=";
}
},
},
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_w = String(context_c);
}
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
}
for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
}
bits = 0;
maxpower = Math.pow(2,2);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
bits = 0;
maxpower = Math.pow(2,numBits);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
switch (c = bits) {
case 0:
bits = 0;
maxpower = Math.pow(2,8);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize-1;
enlargeIn--;
break;
case 1:
bits = 0;
maxpower = Math.pow(2,16);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize-1;
enlargeIn--;
break;
case 2:
return result.join('');
}
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c];
} else {
if (c === dictSize) {
entry = w + w[0];
} else {
return null;
}
}
result.push(entry);
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
}
};
return LZString;
})();
*****LZString.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace XYZ.Helpers
{
public class LZString
{
private class Context_Compress
{
public Dictionary<string, int> dictionary { get; set; }
public Dictionary<string, bool> dictionaryToCreate { get; set; }
public string c { get; set; }
public string wc { get; set; }
public string w { get; set; }
public int enlargeIn { get; set; }
public int dictSize { get; set; }
public int numBits { get; set; }
public Context_Compress_Data data { get; set; }
}
return data;
}
return data;
}
if (context.dictionaryToCreate.ContainsKey(context.w))
{
if (context.w[0] < 256)
{
context.data = writeBits(context.numBits, 0, context.data);
context.data = writeBits(8, context.w[0], context.data);
}
else
{
context.data = writeBits(context.numBits, 1, context.data);
context.data = writeBits(16, context.w[0], context.data);
}
context = decrementEnlargeIn(context);
context.dictionaryToCreate.Remove(context.w);
}
else
{
context.data = writeBits(context.numBits, context.dictionary[context.w], context.data);
}
return context;
}
context.enlargeIn--;
if (context.enlargeIn == 0)
{
context.enlargeIn = (int)Math.Pow(2, context.numBits);
context.numBits++;
}
return context;
}
data.str = "";
data.val = 0;
data.position = 0;
context.data = data;
try
{
for (int i = 0; i < uncompressed.Length; i++)
{
context.c = uncompressed[i].ToString();
if (!context.dictionary.ContainsKey(context.c))
{
context.dictionary[context.c] = context.dictSize++;
context.dictionaryToCreate[context.c] = true;
};
if (context.dictionary.ContainsKey(context.wc))
{
context.w = context.wc;
}
else
{
context = produceW(context);
context = decrementEnlargeIn(context);
context.dictionary[context.wc] = context.dictSize++;
context.w = context.c;
}
}
if (context.w != "")
{
context = produceW(context);
}
}
catch (Exception ex)
{
throw ex;
}
return context.data.str;
}
data.position >>= 1;
if (data.position == 0)
{
data.position = 32768;
// This 'if' check doesn't appear in the orginal lz-string javascript code.
// Added as a check to make sure we don't exceed the length of data.str
// The javascript charCodeAt will return a NaN if it exceeds the index but will not error out
if (data.index < data.str.Length)
{
data.val = data.str[data.index++]; // data.val = data.string.charCodeAt(data.index++);
<---javascript equivilant
}
}
int res = 0;
int maxpower = (int)Math.Pow(2, numBits);
int power = 1;
return res;
}
public static string decompress(string compressed)
{
data.str = compressed;
data.val = (int)compressed[0];
data.position = 32768;
data.index = 1;
try
{
for (i = 0; i < 3; i++)
{
dictionary.Add(i.ToString());
}
switch (next)
{
case 0:
sc = Convert.ToChar(readBits(8, data)).ToString();
break;
case 1:
sc = Convert.ToChar(readBits(16, data)).ToString();
break;
case 2:
return "";
}
dictionary.Add(sc);
result.Append(sc);
w = result.ToString();
while (true)
{
c = readBits(numBits, data);
int cc = c;
switch (cc)
{
case 0:
if (errorCount++ > 10000)
throw new Exception("To many errors");
sc = Convert.ToChar(readBits(8, data)).ToString();
dictionary.Add(sc);
c = dictionary.Count - 1;
enlargeIn--;
break;
case 1:
sc = Convert.ToChar(readBits(16, data)).ToString();
dictionary.Add(sc);
c = dictionary.Count - 1;
enlargeIn--;
break;
case 2:
return result.ToString();
}
if (enlargeIn == 0)
{
enlargeIn = (int)Math.Pow(2, numBits);
numBits++;
}
result.Append(entry);
dictionary.Add(w + entry[0]);
enlargeIn--;
w = entry;
if (enlargeIn == 0)
{
enlargeIn = (int)Math.Pow(2, numBits);
numBits++;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
input = compress(input);
if (input.Length == 0)
return input;
try
{
if (input == null)
throw new Exception("input is Null");
switch (status++)
{
case 0:
current = c << 1;
break;
case 1:
output += (char)(current | (c >> 14));
current = (c & 16383) << 2;
break;
case 2:
output += (char)(current | (c >> 13));
current = (c & 8191) << 3;
break;
case 3:
output += (char)(current | (c >> 12));
current = (c & 4095) << 4;
break;
case 4:
output += (char)(current | (c >> 11));
current = (c & 2047) << 5;
break;
case 5:
output += (char)(current | (c >> 10));
current = (c & 1023) << 6;
break;
case 6:
output += (char)(current | (c >> 9));
current = (c & 511) << 7;
break;
case 7:
output += (char)(current | (c >> 8));
current = (c & 255) << 8;
break;
case 8:
output += (char)(current | (c >> 7));
current = (c & 127) << 9;
break;
case 9:
output += (char)(current | (c >> 6));
current = (c & 63) << 10;
break;
case 10:
output += (char)(current | (c >> 5));
current = (c & 31) << 11;
break;
case 11:
output += (char)(current | (c >> 4));
current = (c & 15) << 12;
break;
case 12:
output += (char)(current | (c >> 3));
current = (c & 7) << 13;
break;
case 13:
output += (char)(current | (c >> 2));
current = (c & 3) << 14;
break;
case 14:
output += (char)(current | (c >> 1));
current = (c & 1) << 15;
break;
case 15:
output += (char)(current | c);
status = 0;
break;
}
i++;
}
}
catch (Exception ex)
{
throw ex;
}
return decompress(output);
}
string _keyStr =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
string output = "";
// Using the data type 'double' for these so that the .Net double.NaN & double.IsNaN functions
can be used
// later in the function. .Net doesn't have a similar function for regular integers.
double chr1, chr2, chr3 = 0.0;
int enc1 = 0;
int enc2 = 0;
int enc3 = 0;
int enc4 = 0;
int i = 0;
try
{
if (input == null)
throw new Exception("input is Null");
input = compress(input);
// The next three 'if' statements are there to make sure we are not trying to calculate a
value that has been
// assigned to 'double.NaN' above. The orginal Javascript functions didn't need these checks
due to how
// Javascript functions.
// Also, due to the fact that some of the variables are of the data type 'double', we have to
do some type
// conversion to get the 'enc' variables to be the correct value.
if (!double.IsNaN(chr2))
{
enc2 = (((int)(Math.Round(chr1)) & 3) << 4) | ((int)(Math.Round(chr2)) >> 4);
}
if (!double.IsNaN(chr3))
{
return output;
}
string _keyStr =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
try
{
if (input == null)
throw new Exception("input is Null");
if (ol % 2 == 0)
{
output_ = chr1 << 8;
if (enc3 != 64)
{
output += (char)(output_ | chr2);
}
if (enc4 != 64)
{
output_ = chr3 << 8;
}
}
else
{
output = output + (char)(output_ | chr1);
if (enc3 != 64)
{
output_ = chr2 << 8;
}
if (enc4 != 64)
{
output += (char)(output_ | chr3);
}
}
ol += 3;
}
return output;
}
}
}
*****GZIP
namespace XYZ.CustomAttributes
{
/// <summary>
/// Attribute that can be added to controller methods to force content
/// to be GZip encoded if the client supports it
/// </summary>
public class CompressContentAttribute : ActionFilterAttribute
{
/// <summary>
/// Override to compress the content that is generated by
/// an action method.
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
GZipEncodePage();
}
/// <summary>
/// Determines if GZip is supported
/// </summary>
/// <returns></returns>
public static bool IsGZipSupported()
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(AcceptEncoding) &&
(AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
return true;
return false;
}
/// <summary>
/// Sets up the current page or handler to use GZip through a Response.Filter
/// IMPORTANT:
/// You have to call this method before any output is generated!
/// </summary>
public static void GZipEncodePage()
{
HttpResponse Response = HttpContext.Current.Response;
if (IsGZipSupported())
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (AcceptEncoding.Contains("gzip"))
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");
}
else
{
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "deflate");
}