aboutsummaryrefslogtreecommitdiffstats
path: root/tracker-stm32/src/compression.cpp
blob: 9b108fda919a32bf3b7528128e01c7f34eb4caca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "compression.h"

using math::wide_integer::uint512_t;

uint512_t decodeBase(int b, const char *str);

const char* digits = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-./?@";

// This function is from aprs434.github.io,
// MIT License, Copyright (c) 2022 Serge Y. Stroobandt, ON4AA
uint32_t encodeCallsign(const char *callsign) {
    // Encode CCCC according to aprs434.github.io/code/codec.cpp
    const int b = 37;

    uint32_t result;
    uint32_t weight;
    int i, j, k, ch;

    result = 0, weight = 1;
    for (i = j = strlen(callsign); i > 0; i--) {  // iterate over input string
        ch = toupper(callsign[i-1]);
        for (k = 0; k < strlen(digits); k++) {    // lookup char index
            if (digits[k] == ch) {
                break;
            }
        }
        if (k == strlen(digits))                  // ignore if ch not found in digits
            continue;
        result = result + ((k) * weight);
        weight = weight * b;
    }

    return result;                                // result can be pow(42,51)!
}

// This function is from ESP32 lora.tracker,
// MIT License, Copyright (c) 2020 Peter Buchegger
void ax25_base91enc(char *s, uint8_t n, uint32_t v) {
    /* Creates a Base-91 representation of the value in v in the string
     * pointed to by s, n-characters long. String length should be n+1.
     */

    for (s += n, *s = '\0'; n; n--) {
        *(--s) = v % 91 + 33;
        v /= 91;
    }
}

static void strrev(char s[])
{
    int length = strlen(s) ;
    int c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

inline void assert(bool cond) {
    while (!cond) {}
}

static uint8_t bytes[100];

uint512_t decodeBase(int b, const char* str) {    // base b, str 64 bytes max '\0' terminated

    uint512_t result;
    uint512_t weight;
    int i, j, k, ch;

    assert(b <= strlen(digits));
    assert(b > 1);

    result = 0, weight = 1;
    for (i = j = strlen(str); i > 0; i--)         // iterate over input string
    {
        ch=toupper(str[i-1]);
        for (k = 0; k < strlen(digits); k++) {    //lookup char index
            if (digits[k] == ch) {
                break;
            }
        }
        if (k == strlen(digits))                  // ignore if ch not found in digits
            continue;
        result = result + ((k) * weight);
        weight = weight * b;
    }

    return result;                                // result can be pow(42,51)!
}

uint512_t encodetttt(const char *s) {
    return decodeBase(42, s);
}