Clicky

20210204

Arduino/ESP Split function

I created the next string "Split" function for Arduino/ESP boards. The performance is OK (25ms/Split)


void setup() {
 
  Serial.begin(115200);
  delay(1000);

  Serial.println("*** Start...");

  String s = "1612248989;Random string with SPACES;192.168.2.2;192.168.3.3;64;16;ABCDE2345678987BCEF";
  Serial.println(s);


  Serial.println(Split(s,";",2));
 
  Serial.println("*** End.");
   
}

void loop() {
  // put your main code here, to run repeatedly:
}


String Split(String aString, String Delimiter, int Index) {
 
  String bString ="", fragment = "";
  int delimiterIndex, i = 0;
 
  bString = aString;  
    
  do {
    i++;
    delimiterIndex = bString.indexOf(Delimiter);
    if (i == Index) {
        fragment = bString.substring(0,delimiterIndex);
    }
    bString = bString.substring(delimiterIndex+1);
    bString.trim();   
  } while (delimiterIndex != -1);
  return fragment;

Result:

18:24:44.626 -> *** Start...
18:24:44.626 -> 1612248989;Random string with SPACES;192.168.2.2;192.168.3.3;64;16;ABCDE2345678987BCEF
18:24:44.626 -> Random string with SPACES
18:24:44.626 -> *** End.
 

20210127

Base85 encode/decode, embedded in Powershell

 The next code embbeds Base85 C#  encode/decode in Powershell.

 ---8< -----------------------------

$base85lib =
@"
using System;
using System.Text;
using System.IO;
using System.Linq;

namespace BaseN {
    /// <summary>
    /// C# implementation of ASCII85 encoding.
    /// Based on C code from http://www.stillhq.com/cgi-bin/cvsweb/ascii85/
    /// </summary>
    /// <remarks>
    /// Jeff Atwood
    /// http://www.codinghorror.com/blog/archives/000410.html
    /// Source code cloned from: https://github.com/coding-horror/ascii85
    /// Modified by Anton Krouglov: EncodeFile and DecodeFile static methods added; namespace added
    /// </remarks>
    public class Ascii85
    {
        /// <summary>
        /// Prefix mark that identifies an encoded ASCII85 string, traditionally '<~'
        /// </summary>
        public string PrefixMark = "<~";
        /// <summary>
        /// Suffix mark that identifies an encoded ASCII85 string, traditionally '~>'
        /// </summary>
        public string SuffixMark = "~>";
        /// <summary>
        /// Maximum line length for encoded ASCII85 string;
        /// set to zero for one unbroken line.
        /// </summary>
        public int LineLength = 75;
        /// <summary>
        /// Add the Prefix and Suffix marks when encoding, and enforce their presence for decoding
        /// </summary>
        public bool EnforceMarks = true;

        private const int _asciiOffset = 33;
        private readonly byte[] _encodedBlock = new byte[5];
        private readonly byte[] _decodedBlock = new byte[4];
        private uint _tuple = 0;
        private int _linePos = 0;

        private readonly uint[] pow85 = { 85*85*85*85, 85*85*85, 85*85, 85, 1 };

        /// <summary>
        /// Decodes an ASCII85 encoded string into the original binary data
        /// </summary>
        /// <param name="s">ASCII85 encoded string</param>
        /// <returns>byte array of decoded binary data</returns>
        public byte[] Decode(string s)
        {
            if (EnforceMarks)
            {
                if (!s.StartsWith(PrefixMark) | !s.EndsWith(SuffixMark))
                {
                    throw new Exception("ASCII85 encoded data should begin with '" + PrefixMark +
                        "' and end with '" + SuffixMark + "'");
                }
            }

            // strip prefix and suffix if present
            if (s.StartsWith(PrefixMark))
            {
                s = s.Substring(PrefixMark.Length);
            }
            if (s.EndsWith(SuffixMark))
            {
                s = s.Substring(0, s.Length - SuffixMark.Length);
            }

            MemoryStream ms = new MemoryStream();
            int count = 0;
            bool processChar = false;

            foreach (char c in s)
            {
                switch (c)
                {
                    case 'z':
                        if (count != 0)
                        {
                            throw new Exception("The character 'z' is invalid inside an ASCII85 block.");
                        }
                        _decodedBlock[0] = 0;
                        _decodedBlock[1] = 0;
                        _decodedBlock[2] = 0;
                        _decodedBlock[3] = 0;
                        ms.Write(_decodedBlock, 0, _decodedBlock.Length);
                        processChar = false;
                        break;
                    case '\n': case '\r': case '\t': case '\0': case '\f': case '\b':
                        processChar = false;
                        break;
                    default:
                        if (c < '!' || c > 'u')
                        {
                            throw new Exception("Bad character '" + c + "' found. ASCII85 only allows characters '!' to 'u'.");
                        }
                        processChar = true;
                        break;
                }

                if (processChar)
                {
                    _tuple += ((uint)(c - _asciiOffset) * pow85[count]);
                    count++;
                    if (count == _encodedBlock.Length)
                    {                   
                        DecodeBlock();
                        ms.Write(_decodedBlock, 0, _decodedBlock.Length);
                        _tuple = 0;
                        count = 0;
                    }                           
                }
            }

            // if we have some bytes left over at the end..
            if (count != 0)
            {
                if (count == 1)
                {
                    throw new Exception("The last block of ASCII85 data cannot be a single byte.");
                }
                count--;
                _tuple += pow85[count];
                DecodeBlock(count);
                for (int i = 0; i < count; i++)
                {
                    ms.WriteByte(_decodedBlock[i]);
                }
            }

            return ms.ToArray();
        }

        /// <summary>
        /// Encodes binary data into a plaintext ASCII85 format string
        /// </summary>
        /// <param name="ba">binary data to encode</param>
        /// <returns>ASCII85 encoded string</returns>
        public string Encode(byte[] ba)
        {
            StringBuilder sb = new StringBuilder((int)(ba.Length * (_encodedBlock.Length/_decodedBlock.Length)));
            _linePos = 0;

            if (EnforceMarks)
            {
                AppendString(sb, PrefixMark);
            }

            int count = 0;
            _tuple = 0;
            foreach (byte b in ba)
            {
                if (count >= _decodedBlock.Length - 1)
                {
                    _tuple |= b;
                    if (_tuple == 0)
                    {
                        AppendChar(sb, 'z');
                    }
                    else
                    {
                        EncodeBlock(sb);
                    }
                    _tuple = 0;
                    count = 0;
                }
                else
                {
                    _tuple |= (uint)(b << (24 - (count * 8)));
                    count++;
                }
            }

            // if we have some bytes left over at the end..
            if (count > 0)
            {
                EncodeBlock(count + 1, sb);
            }

            if (EnforceMarks)
            {
                AppendString(sb, SuffixMark);  
            }
            return sb.ToString();
        }

        private void EncodeBlock(StringBuilder sb)
        {
            EncodeBlock(_encodedBlock.Length, sb);
        }

        private void EncodeBlock(int count, StringBuilder sb)
        {
            for (int i = _encodedBlock.Length - 1; i >= 0; i--)
            {
                _encodedBlock[i] = (byte)((_tuple % 85) + _asciiOffset);
                _tuple /= 85;
            }

            for (int i = 0; i < count; i++)
            {
                char c = (char)_encodedBlock[i];
                AppendChar(sb, c);
            }

        }

        private void DecodeBlock()
        {
            DecodeBlock(_decodedBlock.Length);
        }

        private void DecodeBlock(int bytes)
        {
            for (int i = 0; i < bytes; i++)
            {
                _decodedBlock[i] = (byte)(_tuple >> 24 - (i * 8));  
            }
        }

        private void AppendString(StringBuilder sb, string s)
        {
            if (LineLength > 0 && (_linePos + s.Length > LineLength))
            {
                _linePos = 0;
                sb.Append('\n');
            }
            else
            {
                _linePos += s.Length;
            }
            sb.Append(s);
        }

        private void AppendChar(StringBuilder sb, char c)
        {
            sb.Append(c);
            _linePos++;
            if (LineLength > 0 && (_linePos >= LineLength))
            {
                _linePos = 0;
                sb.Append('\n');
            }
        }

        /// <summary>
        /// Encodes file to base85
        /// </summary>
        /// <param name="inFileName">path to source file</param>
        /// <param name="outFileName">path to result file</param>
        /// <param name="doReverse">reverse base85 straing; default - false</param>
        public static void EncodeFile(string inFileName, string outFileName, bool doReverse = false)
        {
            var encoder = new Ascii85 { EnforceMarks = false, LineLength = 0 };

            // read the file
            byte[] ba;
            using (var streamReader = new FileStream(inFileName, FileMode.Open))
            {
                ba = new byte[streamReader.Length];
                streamReader.Read(ba, 0, (int)streamReader.Length);
                streamReader.Close();
            }

            // encode it
            var encodedString = encoder.Encode(ba);
            //Console.WriteLine("file encoded in string of length " + encodedString.Length);

            var encodedBytes = Encoding.ASCII.GetBytes(encodedString);
            if (doReverse) encodedBytes = encodedBytes.AsEnumerable().Reverse().ToArray();

            // write the file
            using (var streamWriter = new FileStream(outFileName, FileMode.OpenOrCreate))
            {
                streamWriter.Write(encodedBytes, 0, encodedBytes.Length);
                streamWriter.Close();
            }
        }


        /// <summary>
        /// Decodes file from base85
        /// </summary>
        /// <param name="inFileName">path to source file</param>
        /// <param name="outFileName">path to result file</param>
        /// <param name="doReverse">reverse base85 straing; default - false</param>
        public static void DecodeFile(string inFileName, string outFileName, bool doReverse = false)
        {
            var encoder = new Ascii85 { EnforceMarks = false, LineLength = 0 };

            // read the file
            byte[] ba;
            using (var streamReader = new FileStream(inFileName, FileMode.Open))
            {
                ba = new byte[streamReader.Length];
                streamReader.Read(ba, 0, (int)streamReader.Length);
                streamReader.Close();
            }

            if (doReverse) ba = ba.AsEnumerable().Reverse().ToArray();

            var encodedString = Encoding.ASCII.GetString(ba);
        
            // decode it
            var decoded = encoder.Decode(encodedString);
            //Console.WriteLine("decoded file length " + decoded.Length);

            // write the file
            using (var streamWriter = new FileStream(outFileName, FileMode.OpenOrCreate))
            {
                streamWriter.Write(decoded, 0, decoded.Length);
                streamWriter.Close();
            }
        }
    }
}
"@

$ErrorActionPreference = 'Stop'
Add-Type -TypeDefinition $base85lib
[BaseN.Ascii85]::EncodeFile('.\test.lnk', '.\test.lnk.base85')
[BaseN.Ascii85]::DecodeFile('.\test.lnk.base85', '.\test.lnk.result');

 

20201219

CCleaner update script

I am a longtime fan of CCleaner (former CrapCleaner). There is a "free" and a paid version. One difference between free and paid is an automated update of the application. In the newer versions there is an auto update in the free version, but that will show you nag screens.

The next Powershell script checks for a new version on the CCleaner website and when a newer version is available, it will download and install. You run the script manually or by Scheduled Task.


$ccleanserVersion = Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object DisplayName -eq CCleaner | Select -ExpandProperty "DisplayVersion"
write-host "*** Currently installed version: $ccleanserVersion"
$ver = $ccleanserVersion.replace(".","")

$latestVersion = Invoke-WebRequest -UseBasicParsing "https://www.ccleaner.com/ccleaner/download/standard" | Select -ExpandProperty "RawContent"
$versionMatch = $latestVersion -Match "https://download.ccleaner.com/ccsetup$ver.exe"

if (!$versionMatch) {
    write-host "*** Download and install new CCleaner app..."

    $latestVersion = $latestVersion -Match "https://download.ccleaner.com/ccsetup\d{3}.exe"
    $url = $Matches[0]   
    $intallerFile = "c:\temp\ccsetup.exe"

    Import-Module BitsTransfer
    Start-BitsTransfer -Source $url -Destination $intallerFile
    
    write-Output "*** Installing CCleaner..."
    & "C:\Windows\system32\cmd.exe" "/c" "start" "/wait" "$intallerFile" "/S"

    $ccleanserVersion = Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object DisplayName -eq CCleaner | Select -ExpandProperty "DisplayVersion"
    write-host "*** New version: $ccleanserVersion"   
    
}
    ELSE
{
    write-Output "*** No newer version available."
}

The update looks like this:


20201202

Tweakers paywall configuration

Update: 7-Apr-2021 b

Maybe you are aware that Tweakers will deploy a paywall for "Premium" articles. For those who are not interested in T.net links to paywalled articles, you may configure this rule in your favorite content filter:

!Tweakers Plus articles
tweakers.net##.plus
tweakers.net##tr:has(td:has(div.plus))

! Email subscription option on the front page, in the news overview
tweakers.net##.top.frontpage.mainColumn > .darkBlock

! Job offers on the front page
tweakers.net##.bottom.frontpage.secondColumn > .darkBlock

! Advertisements in news articles
tweakers.net##div#layout div#contentArea > div.wrap > div:nth-child(1):has(img[referrerpolicy="unsafe-url"])
tweakers.net##div#top > div#entity > div:nth-child(1) > div:nth-child(1)

! Requests for feedback in news articles
tweakers.net##.usabilla-survey.usabilla-trigger

! Delayed advertisements loaded through JavaScript
@@||tweakers.net^$ghide

This will result in showing "public" links only. Make sure you do not suppress adds, because that is not allowed!

20201031

Convert all FLAC files in a folder to MP3s

The next script takes a folder path as imput, checks all the FLAC files in that folder, and creates MP3 files for each FLAC file found. There is only one prerequisite: FFMPEG should be installed. Save the script as "ConvertFlacToMp3.bat"

Usage:

C:\> ConvertFlacToMp3.bat {path to FLAC files}

or:

Create a shortcut to this script on the desktop and you can drag folders (from Windows Explorer) to the icon.


There are couple of tricks in the script:

- Normalization of the passed folder (e.g. removal of qoutes, required for drag-and-drop)

- Enumeration of FLAC files in the folder

- Filename contruct of the MP3 filename 


You play around with the script and find out yourself. One improvement could be that the MP3 files are stored in a (new) seperate folder. 

 

 ---8< ---------------------------------------------------------------

@echo off

setlocal ENABLEDELAYEDEXPANSION

echo.
echo *** Usage:
echo        C:\^> %0 {path to folder with *.FLAC files}
echo        Or drag the folder with FLAC files to a shortcut of this script.

if %1.==. goto :EOF
if not exist %1 goto :EOF

set cmdParam=%1
set firstChar=%cmdParam:~0,1%
if ^%firstChar% EQU ^"  (
    for /f "tokens=*" %%i in (%1) do set sourceFolder=%%~i
) ELSE (
    set sourceFolder=%1
)   
set SRC=!sourceFolder!

set t=%time%
set ffmpeg=c:\scripts\ffmpeg\bin\ffmpeg.exe

echo.
echo *** Processing folder: "%SRC%"...

for %%i in ("%SRC%\*.flac") do (
    
    set fPath=%%~dpi
    set fName=%%~ni
    
    for %%j in ("!fName!") do set nam=!fPath!!fName!.mp3
    
    echo *** %%i
    %ffmpeg% -y -loglevel panic -i "%%i" -vsync 0 -codec:a libmp3lame -qscale:a 2 "!nam!"

)

echo *** Start: %t%
echo *** End  : %time%

start /separate explorer "%SRC%"

ping -n 60 localhost >nul

---8< ---------------------------------------------------------------

 

Script invocation:


 After the script finishes, Explorer shows the folder with FLAC and MP3 files:



 

 

20200308

Trust comes by foot and goes by horse

In this post, I showed a geolocation lookup method for hMailserver, using a free geolocation webservice. At some point in time this webservice arrived in the DNSBL blacklist, and the geolocation lookups failed.

I do not have a clue why this service was placed on the blacklist, but during the time that it worked, I noticed that some malicious IP addresses were mapped to NL/Amsterdam instead of the country/locations that other geolocation services provide (so the trust in de geolocation webservice is lost here).

There are two things that we can do: place the existing geolocation service on the whitelist or use a local geolocation database. Since IPv4 addresses/subnets will not change that fast (anymore), it is a feasable solution to use a local database for lookups. So here we go!

You need to download the database itself and a (command line) tool to query the database. Create an account with Maxmind (https://www.maxmind.com/en/home) and download GeoLite2-Country.mmdb and mmdbinspect.exe

This is the modified geolookup function:

    function IPtoGeoLocal(IPaddr)

        const geoDbPath     = "{path to}\GeoLite2-Country.mmdb"
        const geoLookupExe  = "
{path to}\mmdbinspect.exe"
        const tempPath      = "c:\temp"
        const searchString  = "iso_code"
       
        dim wsh, fso
        set wsh = createobject("wscript.shell")
        set fso = createobject("scripting.filesystemobject")
       
        '--- Create a temp file with a unique filename to prevent conflicts...
        tempFile = tempPath & "\" & IPaddr & ".dat"
        geoLoc = "XX"

        '--- Invoke Maxmind command line tool, do a lookup and pipe the result in tempFile...
        runString = "cmd /c " & geoLookupExe & " --db " & geoDbPath & " " & IPaddr & " | find """ & searchString & """ > " & tempFile
        wsh.run runString,0,true
       
        '--- Read one line from tempFile...
        set f = fso.openTextFile(tempFile) : s = f.readLine : f.Close
        fso.deleteFile tempFile, true
       
        t = split(s, chr(34))
        if Instr(s, searchString) > 0 then IPtoGeoLocal = t(3)   
       
    end function


When the function is called:

wscript.echo IPtoGeoLocal("8.8.8.8")

You will get the two character ISO3166 code back ("US") or "XX" when the IP address is not found in the database. 

20200303

Capacitor Plague, deel 3

Vervolg op deel 1 en deel 2.

Op mijn studeerkamer hangt al een jaar of 9 een LG TV. Die begon kuren te vertonen. Als 'ie aan werd gezet met de afstandsbediening hoorde je het relais schakelen, maar de TV ging niet aan. Alleen nadat de voedingstekker ongeveer 15 seconden uit de wandcontactdoos gehaald was, en het lichtje op de voorkant van de TV gedoofd, kon de TV weer aangezet worden.

Een mogelijk geval van "brownout". De electronica bestaat uit een voedingsprint en een TV print. Op de voedingsprint zit een 5VDC voeding met twee elco's die bol stonden en electroliet lekte:



Twee vervangende elco's gesoldeerd en de TV werkte weer als een zonnetje:


Wat opviel was dat de twee originele elco's (merk: Sam Young, type: NXT, 1500uF, 6.3V) de enige van dit merk en type waren op de gehele voedingsprint. Het specificatieblad van deze serie elco laat ook zien dat 'ie zeer slechte specificaties heeft. Terwijl de overige elco's van betere kwaliteit zijn. Dit doet sterk vermoeden dat LG hier bezig is geweest met geplande veroudering.

Hoe dan ook, dit is toch de laatste LG TV vanwege dit akkefietje en een aantal andere zaken met LG die niet in de haak waren.

P.S. ik ben niet de enige met precies hetzelfde probleem in een LG TV: https://www.youtube.com/watch?v=eZna3Fj3O4Q&feature=youtu.be 



Real Time Web Analytics