Is it possible to decode a SPAMCAUSE field in a mail header? - decode

I'd like to decode this string:
X-OVH-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeelgedrvdduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfenuceurghilhhouhhtmecufedttdenucgohfhorhgsihguuggvnhfjughrucdlhedttddm
How can I do this?

There is a Tor hidden service you can use to decode the tag located at http://6jbnmws2zq2m2fsfmpwnssgsrxovohgggphymkd4df2pgcw7ccrdy6ad.onion
According to it, the X-OVH-SPAMCAUSE you gave translates to this:
Vade Retro 01.394.21 AS+AV+AP+RT Profile: OVH; Bailout: 300; ^ForbiddenHdr (500)

Starting from lkraider's great Python answer, I improved the accuracy. It turns out that the offset characters (c..g) are alternately appended and prepended. So instead of just checking if one of them is in the pair, it is necessary to differentiate between, e.g., fh and hf, by keeping track of even or odd pairs.
def decode(msg):
text = ""
for i in range(0, len(msg), 2):
# add position as extra parameter
text += unrot(msg[i: i + 2], i // 2)
return text
def unrot(pair, pos, key=ord('x')):
# "even" position => 2nd char is offset
if pos % 2 == 0:
# swap letters in pair
pair = pair[1] + pair[0]
# treat 1st char as offset
offset = (ord('g') - ord(pair[0])) * 16
# map to original character
return chr(sum(ord(c) for c in pair) - key - offset)
print(decode('gggruggvucftvghtrhho'))
https://gist.github.com/DoubleYouEl/e3de97293ce3d5452b3be7a336a06ad7

Looks to be some obfuscation by rotating chars. I made an attempt at it using Python. It's not perfect but mostly seems to work:
def decode(msg):
text = []
for i in range(0, len(msg), 2):
text.append(unrot(msg[i: i + 2]))
return str.join('', text)
def unrot(pair, key=ord('x')):
offset = 0
for c in 'cdefgh':
if c in pair:
offset = (ord('g') - ord(c)) * 16
break
return chr(sum(ord(c) for c in pair) - key - offset)
print(decode('gggruggvucftvghtrhho'))
https://gist.github.com/lkraider/9530798a695586fc1580d0728966f6f0

I improved the given Python solutions by Ikraider and DoubleYou and added a JavaScript solution, too.
Python:
def Decode(msg):
return ''.join([chr(ord(msg[i * 2]) + ord(msg[i * 2 + 1]) - 1768 + ord(msg[i * 2 + 1 - (i & 1)]) * 16) for i in range(len(msg) // 2)])
print(Decode('gggruggvucftvghtrhho'))
JavaScript:
function Decode(msg)
{
return Array(msg.length >> 1).fill(0).map((_, i) => String.fromCharCode(msg[i * 2].charCodeAt(0) + msg[i * 2 + 1].charCodeAt(0) - 1768 + (msg[i * 2 + 1 - (i & 1)].charCodeAt(0) << 4))).join('');
}
console.log(Decode('gggruggvucftvghtrhho'));

Related

Maxima: How to factor out a finite sum?

This question is about humane representation of Maxima output.
In short, how do I make
b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)
look like
b*sum_{i=1}^{4} d_i + a*sum_{j=1}^{4}sin(c_i + \alpha)
where sum_{*}^{*}* is a summation sign and an expression with subscripts ?
Or deeper, how to properly model a finite set of items here ?
Consider a finite set of entities $x_i$ (trying to speak tex here) that are numbered from 1 to n where n is known. Let a function $F$ depend on several characteristics of those entities $c_ji = c_j(x_i), j = 1..k$ (k - also known) so that $F = F(c_11,...,c_kn)$.
Now when I try to implement that in Maxima and do things with it, it would yield sums and products of all kinds, where the numbered items are represented something like $c_1*a + c_2*a + c_3*a + c_4*a + d_1*b + d_2*b + d_3*b + d_4*b$ which you would write down on paper as $a*\sum_{i=1}^{4}c_i + b*sum_{i=1}^{4}d_i$.
So how can I make Maxima do that sort of expression contraction ?
To be more specific, here is an actual code example:
(Maxima output marked as ">>>")
/* let's have 4 entities: */
n: 4 $
/* F is a sum of similar components corresponding to each entity F = F_1 + F_2 + F_3 + F_4 */
F_i: a*sin(alpha + c_i) + b*d_i;
>>> b*d_i + a*sin(c_i + alpha)
/* defining the characteristics */
c(i) := concat(c, i) $
d(i) := concat(d, i) $
/* now let's see what F looks like */
/* first, we should model the fact that we have 4 entities somehow: */
F_i(i) := subst(c(i), c_i, subst(d(i), d_i, F_i)) $
/* now we can evaluate F: */
F: sum(F_i(i), i, 1, 4);
>>> b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)
/* at this point it would be nice to do something like: */
/* pretty(F); */
/* and get an output of: */
/* $b*\sum_{i=1}^{4}d_i + a*\sum_{j=1}^4 sin(c_j + \alpha)$ */
/* not to mention having Maxima write things in the same order as I do */
So, to sum up, there are three quetions here:
How do I factor out a sum from an expression like the one on top of this post ?
How do I properly let Maxima know what I'm speaking about here ?
How to make Maxima preserve my order of things in output ?
Thanks in advance.
Here's a way to go about what I think you want.
(%i1) n: 4 $
(%i2) F(i) := a*sin(alpha + c[i]) + b*d[i];
(%o2) F(i) := a sin(alpha + c ) + b d
i i
(%i3) 'sum(F(i), i,1,4);
4
====
\
(%o3) > (a sin(c + alpha) + b d )
/ i i
====
i = 1
(%i4) declare (nounify(sum), linear);
(%o4) done
(%i5) 'sum(F(i), i,1,4);
4 4
==== ====
\ \
(%o5) a > sin(c + alpha) + b > d
/ i / i
==== ====
i = 1 i = 1
(%i6)
The most important thing here is that I've written what we would call "c sub i" and "d sub i" as c[i] and d[i] respectively. These are indexed variables named c and d, and i is the index. It's not necessary for there to actually exist arrays or lists named c or d, and i might or might not have a specific value.
I have written F as an ordinary function. I've avoided the construction of variable names via concat and avoided the substitution of those names into an expression. I would like to emphasize that such operations are almost certainly not the best way to go about it.
In %i3 note that I wrote the summation as 'sum(...) which makes it a so-called noun expression, which means it is maintained in a symbolic form and not evaluated.
By default, summations are not treated as linear, so in %i4 I declared summations as linear so that the result in %o5 is as expected.
Maxima doesn't have a way to collect expressions such as a1 + a2 + a3 back into a symbolic summation, but perhaps you don't need such an operation.

When dealing with integer division, is there a way to collect like terms?

For example, when NOT working with integer division the following is true
x/4 + x/2 = x*(1/4+1/2) = x * 3/4
When dealing with integer division is there a way to reduce x/4 + x/2 into this form:
x * (int1/int2)? If so, how?
The question reduce x/4 + x/2 into this form: x * (int1/int2) appears to be not quite the query you want. Forcing the (int1/int2) division first simple results in int3.
So let's work with
reduce x/4 + x/2 into this form: (x * int1)/int2
As others have mentioned, there are issues with this that hints to its impossibility. So I'll propose yet another form that might work for you in that it is still one access to x and no branching.
reduce x/4 + x/2 into this form: ((x/int1)*int2)/int3
x/4 + x/2 reduces to ((x/2)*3)/2. It takes advantage that 4 is a multiple of 2.
Note: There remains a possibility of overflow for large |x| beginning with INTMAX/3*2 or so.
Test code
int test2(int x) {
int y1 = x/4 + x/2;
int y2 = ((x/2)*3)/2;
printf("%3d %3d %3d %d\n", x, y1, y2, y1==y2);
return y1==y2;
}
I don't think you'll be able to do this. Take for example
5 \ 3 + 5 \ 2 = 1 + 2 = 3
where \ denotes integer division.
Now look at the same expression with regular division
a / b + a / c = a(b + c) / bc
If we were to try to apply this rule to the example above, substituting \ for /, we would get this:
5 \ 3 + 5 \ 2 = 5(3 + 2) \ (2 * 3) = 25 \ 6 = 4 [wrong answer!]
^^^
This must be wrong
I'm not trying to make the claim that there doesn't exist some identity similar to this that is correct.

interpolation point by pixels

I need to offset one point on line by pixels.
public static function interpolate(pt1:Point, pt2:Point, f:Number):Point
{
var x:Number = f * pt1.x + (1 - f) * pt2.x;
var y:Number = f * pt1.y + (1 - f) * pt2.y;
return new Point(x, y);
}
This function can interpolate point by percent if "f" is 0.5 the point will be in the center of line pt1 pt2. Is there a way to make this with pixels?
it sounds like you want to write a method interpolate(pt1, pt2, distanceFromPt1). Your existing interpolate method does something similar, so you can use the latter to implement the former.
Right now, if you call interpolate(A,B,f), you get a point D where (distanceBetween(A,D) / distanceBetween(A,B)) == 1-f. In the version of interpolate you want to write, you don't know what f should be, but you can solve for it, because you know distanceBetween(A,D).
function iterpolateByDistance(A, B, distanceFromA){
//(dist(A,D) / dist(A,B)) == 1-f
//f + (dist(A,D) / dist(A,B)) == 1
//f == 1 - (dist(A,D) / dist(A,B))
f = 1 - (distanceFromPt1 / distanceBetween(A, B));
return interpolate(A, B, f);
}

Conversion of Double to value digits and exponent

For ex.
double size = 10.35;
i should get
value = 1035;
exponent = -2;
so when i re calculate i will get 10.35.
i.e 1035 * 10^-2 = 10.35;
Please help me.
Thanks in advance
In general this is not possible since the fractional part of a double is stored in powers-of-2, and might or might not match powers-of-10.
For example: When looking at powers-of-2 vs powers-of-3: Just like 1/2 == 2^-1 == 5 * 10^-1 has a match, 1/3 == 3^-1 == ?? does not have a match.
However, you can approximate it.
It would have an answer if you would ask for powers-of-2. In that case you can just look at the double representation (see IEEE-754 here) and extract the right bits.
Very simplistically (in C#):
double size = 10.36;
int power = 0;
while (size != (int)size)
{
size *= 10.0;
power--;
}
Console.WriteLine("{0} * 10 to the {1}", size, power);
Though I'm sure with a bit more thought a more elegant solution can be found.
This doesn't go the other way where you've got a large number (103600 say) and want to get the smallest value to some power (1036 * 10^2).
I had to do something very similar. Here's a solution in Python (it hasn't been tested very well):
def normalize(value, fdigits=2):
"""
Convert a string representing a numerical value to value-digit/exponent form.
Round the fractional portion to the given number of digits.
value the value (string)
fdigits the number of digits to which to round the fractional
portion
"""
# if empty string, return error
if not value:
return None
# split value by decimal
v = value.split('.')
# if too many decimals, return error
if len(v) > 2:
return None
# add empty string for fractional portion if missing
elif len(v) == 1:
v.append('')
# assign whole and fractional portions
(w, f) = v
# pad fractional portion up to number of significant digits if necessary
if len(f) < fdigits:
f += ('0' * (fdigits - len(f)))
# if the number of digits in the fractional portion exceeds the
# number of digits allowed by fdigits
elif len(f) > fdigits:
# convert both portions to integers; use '0' for whole portion if missing
(wi, fi) = (int(w or '0'), int(f[:fdigits]))
# round up if first insignificant digit is gteq 5
if int(f[fdigits]) >= 5:
fi += 1
# roll whole value up if fractional portion rounds to a whole
if len(str(fi)) > fdigits:
wi += 1
fi = 0
# replace the whole and fractional strings
(w, f) = (str(wi), ("%0" + str(fdigits) + "d") % fi)
# derive value digits and exponent
n = w.lstrip() + f
l = len(n)
x = -fdigits
n = n.rstrip('0')
x += (l - len(n))
# return value digits and exponent
return (int(n), x)

Math - mapping numbers

How do I map numbers, linearly, between a and b to go between c and d.
That is, I want numbers between 2 and 6 to map to numbers between 10 and 20... but I need the generalized case.
My brain is fried.
If your number X falls between A and B, and you would like Y to fall between C and D, you can apply the following linear transform:
Y = (X-A)/(B-A) * (D-C) + C
That should give you what you want, although your question is a little ambiguous, since you could also map the interval in the reverse direction. Just watch out for division by zero and you should be OK.
Divide to get the ratio between the sizes of the two ranges, then subtract the starting value of your inital range, multiply by the ratio and add the starting value of your second range. In other words,
R = (20 - 10) / (6 - 2)
y = (x - 2) * R + 10
This evenly spreads the numbers from the first range in the second range.
It would be nice to have this functionality in the java.lang.Math class, as this is such a widely required function and is available in other languages.
Here is a simple implementation:
final static double EPSILON = 1e-12;
public static double map(double valueCoord1,
double startCoord1, double endCoord1,
double startCoord2, double endCoord2) {
if (Math.abs(endCoord1 - startCoord1) < EPSILON) {
throw new ArithmeticException("/ 0");
}
double offset = startCoord2;
double ratio = (endCoord2 - startCoord2) / (endCoord1 - startCoord1);
return ratio * (valueCoord1 - startCoord1) + offset;
}
I am putting this code here as a reference for future myself and may be it will help someone.
As an aside, this is the same problem as the classic convert celcius to farenheit where you want to map a number range that equates 0 - 100 (C) to 32 - 212 (F).
https://rosettacode.org/wiki/Map_range
[a1, a2] => [b1, b2]
if s in range of [a1, a2]
then t which will be in range of [b1, b2]
t= b1 + ((s- a1) * (b2-b1))/ (a2-a1)
In addition to #PeterAllenWebb answer, if you would like to reverse back the result use the following:
reverseX = (B-A)*(Y-C)/(D-C) + A
Each unit interval on the first range takes up (d-c)/(b-a) "space" on the second range.
Pseudo:
var interval = (d-c)/(b-a)
for n = 0 to (b - a)
print c + n*interval
How you handle the rounding is up to you.
if your range from [a to b] and you want to map it in [c to d] where x is the value you want to map
use this formula (linear mapping)
double R = (d-c)/(b-a)
double y = c+(x*R)+R
return(y)
Where X is the number to map from A-B to C-D, and Y is the result:
Take the linear interpolation formula, lerp(a,b,m)=a+(m*(b-a)), and put C and D in place of a and b to get Y=C+(m*(D-C)). Then, in place of m, put (X-A)/(B-A) to get Y=C+(((X-A)/(B-A))*(D-C)). This is an okay map function, but it can be simplified. Take the (D-C) piece, and put it inside the dividend to get Y=C+(((X-A)*(D-C))/(B-A)). This gives us another piece we can simplify, (X-A)*(D-C), which equates to (X*D)-(X*C)-(A*D)+(A*C). Pop that in, and you get Y=C+(((X*D)-(X*C)-(A*D)+(A*C))/(B-A)). The next thing you need to do is add in the +C bit. To do that, you multiply C by (B-A) to get ((B*C)-(A*C)), and move it into the dividend to get Y=(((X*D)-(X*C)-(A*D)+(A*C)+(B*C)-(A*C))/(B-A)). This is redundant, containing both a +(A*C) and a -(A*C), which cancel each other out. Remove them, and you get a final result of: Y=((X*D)-(X*C)-(A*D)+(B*C))/(B-A)
TL;DR: The standard map function, Y=C+(((X-A)/(B-A))*(D-C)), can be simplified down to Y=((X*D)-(X*C)-(A*D)+(B*C))/(B-A)
int srcMin = 2, srcMax = 6;
int tgtMin = 10, tgtMax = 20;
int nb = srcMax - srcMin;
int range = tgtMax - tgtMin;
float rate = (float) range / (float) nb;
println(srcMin + " > " + tgtMin);
float stepF = tgtMin;
for (int i = 1; i < nb; i++)
{
stepF += rate;
println((srcMin + i) + " > " + (int) (stepF + 0.5) + " (" + stepF + ")");
}
println(srcMax + " > " + tgtMax);
With checks on divide by zero, of course.

Resources