User:RhoSigma/Test: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Reverted
Line 1: Line 1:
== ATTENTION ==
== ATTENTION ==
'''Keep Out''' - This is my all things MediaWiki '''Nuke Test Site''' ...
'''Keep Out''' - This is my all things MediaWiki '''Nuke Test Site''' ...
=== base64.h ===
{{TextStart}}
//this file is part of MimeTools (plugin for Notepad++)
//Copyright (C)2019 Don HO <don.h@free.fr>
//
//
// Enhance Base64 features, and rewrite Base64 encode/decode implementation
// Copyright 2019 by Paul Nankervis <paulnank@hotmail.com>
//
// Added "RSQB" include guard and namespace for use in QB64, Feb-5th-2022
// Done by RhoSigma, Roland Heyder <support@rhosigma-cw.net>
//
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// Base64 encoding decoding - where 8 bit ascii is re-represented using just 64 ascii characters (plus optional padding '=').
//
// This code includes options to encode to base64 in multiple ways. For example the text lines:-
//
//  If you can keep your head when all about you
//  Are losing theirs and blaming it on you;
//
// Using "Encode with Unix EOL" would produce a single base64 string with line breaks after each 64 characters:-
//
//  SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3UNCkFy
//  ZSBsb3NpbmcgdGhlaXJzIGFuZCBibGFtaW5nIGl0IG9uIHlvdTs=
//
// That would be decoded using a single base64 decode which ignored whitespace characters (the line breaks).
//
// Alternatively the same lines could be encoded using a "by line" option to encode each line of input as
// its own separate base64 string:-
//
//  SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3U
//  QXJlIGxvc2luZyB0aGVpcnMgYW5kIGJsYW1pbmcgaXQgb24geW91Ow
//
// Each of these output lines could be decoded separately, or multiple lines decoded using "reset on whitespace"
// to cause base64 decoding to restart on each line
#ifndef RSQB_BASE64_H // include only once
#define RSQB_BASE64_H
namespace rsqbbase64 {
char base64CharSet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int base64CharMap[] = {  // base64 values or: -1 for illegal character, -2 to ignore character, and -3 for pad ('=')
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,  // <tab> <lf> & <cr> are ignored
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,  // <space> is ignored
    52, 53, 54, 55 ,56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,  // '=' is the pad character
    -1,  0,  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, -1, -1, -1, -1 ,-1,
    -1, 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, -1, -1, -1, -1, -1
};
// base64Encode simply converts ascii to base64 with appropriate wrapping and padding. Encoding is done by loading
// three ascii characters at a time into a bitField, and then extracting them as four base64 values.
// returnString is assumed to be large enough to contain the result (which is typically 4 / 3 the input size
// plus line breaks), and the function return is the length of the result
// wrapLength sets the length at which to wrap the encoded test at (not valid with byLineFlag)
// padFlag controls whether the one or two '=' pad characters are included at the end of encoding
// byLineFlag causes each input line to be encoded as a separate base64 string
int base64Encode(char *resultString, const char *asciiString, size_t asciiStringLength, size_t wrapLength, bool padFlag, bool byLineFlag)
{
    size_t index; // input string index
    size_t lineLength = 0; // current line length
    int resultLength = 0, // result string length
        bitField, // assembled bit field (up to 3 ascii characters at a time)
        bitOffset = -1, // offset into bit field (8 bit input: 16, 8, 0 -> 6 bit output: 18, 12, 6, 0)
        endOffset, // end offset index value
        charValue; // character value
    for (index = 0; index < asciiStringLength; )
    {
        bitField = 0;
        for (bitOffset = 16; bitOffset >= 0 && index < asciiStringLength; bitOffset -= 8)
        {
            charValue = (UCHAR)asciiString[index];
            if (byLineFlag && (charValue == '\n' || charValue == '\r'))
            {
                break;
            }
            index++;
            bitField |= charValue << bitOffset;
        }
        endOffset = bitOffset + 3; // end indicator
        for (bitOffset = 18; bitOffset > endOffset; bitOffset -= 6)
        {
            if (wrapLength > 0 && lineLength++ >= wrapLength && !byLineFlag)
            {
                resultString[resultLength++] = '\n';
                lineLength = 1;
            }
            resultString[resultLength++] = base64CharSet[(bitField >> bitOffset) & 0x3f];
        }
        if (byLineFlag)
        {
            while (index < asciiStringLength && (asciiString[index] == '\n' || asciiString[index] == '\r'))
            {
                resultString[resultLength++] = asciiString[index++];
            }
        }
    }
    if (padFlag && !byLineFlag)
    {
        for (; bitOffset >= 0; bitOffset -= 6)
        {
            if (wrapLength > 0 && lineLength++ >= wrapLength)
            {
                resultString[resultLength++] = '\n';
                lineLength = 1;
            }
            resultString[resultLength++] = '=';
        }
    }
    return resultLength;
}
// base64Decode converts base64 to ascii. But there are choices about what to do with illegal characters or
// malformed strings. In this version there is a strict flag to indicate that the input must be a single
// valid base64 string with no illegal characters, no extra padding, and no short segments. Otherwise
// there is best effort to decode around illegal characters which ARE preserved in the output.
// So  "TWFyeQ==.aGFk.YQ.bGl0dGxl.bGFtYg=="  decodes to  "Mary.had.a.little.lamb"  with five seperate
// base64 strings decoded, each separated by the illegal character dot. In strict mode the first dot
// would trigger a fatal error. Some other implementations choose to ignore illegal characters which
// of course has it's own issues.
// The four whitespace characters <CR> <LF> <TAB> and <SPACE> are silently ignored unless noWhitespaceFlag
// is set. In this case whitespace is treated similar to illegal characters and base64 decoding operates
// around the white space. So "TWFyeQ== aGFk YQ bGl0dGxl bGFtYg==" would decode as "Mary had a little lamb".
// Decoding is done by loading four base64 characters at a time into a bitField, and then extracting them as
// three ascii characters.
// returnString is assumed to be large enough to contain the result (which could be the same size as the input),
// and the function return is the length of the result, or a negative value in case of an error
int base64Decode(char *resultString, const char *encodedString, size_t encodedStringLength, bool strictFlag, bool whitespaceReset)
{
    size_t index; // input string index
    int resultLength = 0, // result string length
        bitField, // assembled bit field (up to 3 ascii characters at a time)
        bitOffset, // offset into bit field (6 bit intput: 18, 12, 6, 0 -> 8 bit output: 16, 8, 0)
        endOffset, // end offset index value
        charValue = 0, // character value
        charIndex = 0, // character index
        padLength = 0; // pad characters seen
    for (index = 0; index < encodedStringLength; )
    {
        bitField = 0;
        for (bitOffset = 18; bitOffset >= 0 && index < encodedStringLength; )
        {
            charValue = (UCHAR)encodedString[index++];
            charIndex = base64CharMap[charValue & 0x7f];
            if (charIndex >= 0)
            {
                if (padLength > 0 && strictFlag)
                {
                    return -1; // **ERROR** Data after pad character
                }
                bitField |= charIndex << bitOffset;
                bitOffset -= 6;
            }
            else
            {
                if (charIndex == -3) // -3 is Pad character '='
                {
                    padLength++;
                    if (strictFlag && bitOffset > 6)
                    {
                        return -2; // **ERROR** Pad character in wrong place
                    }
                }
                else // either -1 for illegal character or -2 for whitespace (ignored)
                {
                    if (charIndex == -1 || whitespaceReset)
                    {
                        charIndex = -1; // Remember it as an illegal character for copy below
                        break;  // exit loop to deal with illegal character
                    }
                }
            }
        }
        if (strictFlag && bitOffset == 12)
        {
            return -3; // **ERROR** Single symbol block not valid
        }
        endOffset = bitOffset + 3; // end indicator
        for (bitOffset = 16; bitOffset > endOffset; bitOffset -= 8)
        {
            resultString[resultLength++] = (bitField >> bitOffset) & 0xff;
        }
        if (charIndex == -1) // Was there an illegal character?
        {
            if (strictFlag)
            {
                return -4; // **ERROR** Bad character in input string
            }
            resultString[resultLength++] = (char)charValue;
        }
    }
    return resultLength;
}
} // namespace
#endif // RSQB_BASE64_H
{{TextEnd}}
=== base64.bi ===
{{CodeStart}}
{{Text|<nowiki>'+---------------+---------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'| ###### ###### |    .--. .        .-.                            |</nowiki>|#919191}}
{{Text|<nowiki>'| ##  ## ##  # |    |  )|        (  ) o                        |</nowiki>|#919191}}
{{Text|<nowiki>'| ##  ##  ##    |    |--' |--. .-.  `-.  .  .-...--.--. .-.        |</nowiki>|#919191}}
{{Text|<nowiki>'| ######  ##  |    |  \ |  |(  )(  ) | (  ||  |  |(  )      |</nowiki>|#919191}}
{{Text|<nowiki>'| ##      ##    |    '  `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |</nowiki>|#919191}}
{{Text|<nowiki>'| ##    ##  # |                            ._.'                  |</nowiki>|#919191}}
{{Text|<nowiki>'| ##    ###### |  Sources & Documents placed under GNU GPL v2.0  |</nowiki>|#919191}}
{{Text|<nowiki>'+---------------+---------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'| === base64.bi ===                                                |</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'| == Definitions required for the routines provided in base64.bm.  |</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'+-------------------------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |</nowiki>|#919191}}
{{Text|<nowiki>'| Find me in the QB64 Forum or mail to support@rhosigma-cw.net for  |</nowiki>|#919191}}
{{Text|<nowiki>'| any questions or suggestions. Thanx for your interest in my work. |</nowiki>|#919191}}
{{Text|<nowiki>'+-------------------------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>' This program is free software; you can redistribute it and/or</nowiki>|#919191}}
{{Text|<nowiki>' modify it under the terms of the GNU General Public License</nowiki>|#919191}}
{{Text|<nowiki>' as published by the Free Software Foundation; either</nowiki>|#919191}}
{{Text|<nowiki>' version 2 of the License, or (at your option) any later version.</nowiki>|#919191}}
{{Text|<nowiki>'</nowiki>|#919191}}
{{Text|<nowiki>' This program is distributed in the hope that it will be useful,</nowiki>|#919191}}
{{Text|<nowiki>' but WITHOUT ANY WARRANTY; without even the implied warranty of</nowiki>|#919191}}
{{Text|<nowiki>' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</nowiki>|#919191}}
{{Text|<nowiki>' GNU General Public License for more details.</nowiki>|#919191}}
{{Text|<nowiki>'</nowiki>|#919191}}
{{Text|<nowiki>' You should have received a copy of the GNU General Public License</nowiki>|#919191}}
{{Text|<nowiki>' along with this program; if not, write to the Free Software</nowiki>|#919191}}
{{Text|<nowiki>' Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</nowiki>|#919191}}
{{Text|<nowiki>'--- Helper functions defined in base64.h, which should reside (along with</nowiki>|#919191}}
{{Text|<nowiki>'--- the respective .bi/.bm files) in the main QB64 installation folder.</nowiki>|#919191}}
{{Text|<nowiki>'-----</nowiki>|#919191}}
{{Text|<nowiki>'--- If you rather place your library files in a sub-directory (as I do),</nowiki>|#919191}}
{{Text|<nowiki>'--- then you must specify the path within the DECLARE LIBRARY statement</nowiki>|#919191}}
{{Text|<nowiki>'--- assuming the main QB64 installation folder as root.</nowiki>|#919191}}
{{Text|<nowiki>'-----</nowiki>|#919191}}
{{Cl|DECLARE LIBRARY}} {{Text|<nowiki>"QB64Library\B64-Coding\base64"</nowiki>|#FFB100}} {{Text|<nowiki>'Do not add .h here !!</nowiki>|#919191}}
    {{Cl|FUNCTION}} {{Text|B64Enc&|#55FF55}} {{Cl|ALIAS}} {{Text|<nowiki>"rsqbbase64::base64Encode"</nowiki>|#FFB100}} (resultString$, asciiString$, {{Cl|BYVAL}} asciiStringLength&, {{Cl|BYVAL}} wrapLength&, {{Cl|BYVAL}} padFlag&, {{Cl|BYVAL}} byLineFlag&)
    {{Cl|FUNCTION}} {{Text|B64Dec&|#55FF55}} {{Cl|ALIAS}} {{Text|<nowiki>"rsqbbase64::base64Decode"</nowiki>|#FFB100}} (resultString$, encodedString$, {{Cl|BYVAL}} encodedStringLength&, {{Cl|BYVAL}} strictFlag&, {{Cl|BYVAL}} whitespaceReset&)
    {{Text|<nowiki>'The low level wrappers to Base64, you should not use this directly,</nowiki>|#919191}}
    {{Text|<nowiki>'rather use the QB64 FUNCTIONs provided in the base64.bm include.</nowiki>|#919191}}
{{Cl|END DECLARE}}
{{Text|<nowiki>'--- Possible Base64 decoding errors</nowiki>|#919191}}
{{Text|<nowiki>'-----</nowiki>|#919191}}
{{Cl|CONST}} B64DEC_ERROR_DAP = {{Text|-1|#F580B1}} {{Text|<nowiki>'Data after pad character</nowiki>|#919191}}
{{Cl|CONST}} B64DEC_ERROR_PCW = {{Text|-2|#F580B1}} {{Text|<nowiki>'Pad character in wrong place</nowiki>|#919191}}
{{Cl|CONST}} B64DEC_ERROR_SSB = {{Text|-3|#F580B1}} {{Text|<nowiki>'Single symbol block not valid</nowiki>|#919191}}
{{Cl|CONST}} B64DEC_ERROR_BCI = {{Text|-4|#F580B1}} {{Text|<nowiki>'Bad character in input string</nowiki>|#919191}}
{{CodeEnd}}
=== base64.bm ===
{{CodeStart}}
{{Text|<nowiki>'+---------------+---------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'| ###### ###### |    .--. .        .-.                            |</nowiki>|#919191}}
{{Text|<nowiki>'| ##  ## ##  # |    |  )|        (  ) o                        |</nowiki>|#919191}}
{{Text|<nowiki>'| ##  ##  ##    |    |--' |--. .-.  `-.  .  .-...--.--. .-.        |</nowiki>|#919191}}
{{Text|<nowiki>'| ######  ##  |    |  \ |  |(  )(  ) | (  ||  |  |(  )      |</nowiki>|#919191}}
{{Text|<nowiki>'| ##      ##    |    '  `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |</nowiki>|#919191}}
{{Text|<nowiki>'| ##    ##  # |                            ._.'                  |</nowiki>|#919191}}
{{Text|<nowiki>'| ##    ###### |  Sources & Documents placed under GNU GPL v2.0  |</nowiki>|#919191}}
{{Text|<nowiki>'+---------------+---------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'| === base64.bm ===                                                |</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'| == The library provides FUNCTIONs for Base64 en-/decoding in QB64.|</nowiki>|#919191}}
{{Text|<nowiki>'| == Encoding and decoding is done from string to string.          |</nowiki>|#919191}}
{{Text|<nowiki>'|                                                                  |</nowiki>|#919191}}
{{Text|<nowiki>'+-------------------------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>'| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |</nowiki>|#919191}}
{{Text|<nowiki>'| Find me in the QB64 Forum or mail to support@rhosigma-cw.net for  |</nowiki>|#919191}}
{{Text|<nowiki>'| any questions or suggestions. Thanx for your interest in my work. |</nowiki>|#919191}}
{{Text|<nowiki>'+-------------------------------------------------------------------+</nowiki>|#919191}}
{{Text|<nowiki>' This program is free software; you can redistribute it and/or</nowiki>|#919191}}
{{Text|<nowiki>' modify it under the terms of the GNU General Public License</nowiki>|#919191}}
{{Text|<nowiki>' as published by the Free Software Foundation; either</nowiki>|#919191}}
{{Text|<nowiki>' version 2 of the License, or (at your option) any later version.</nowiki>|#919191}}
{{Text|<nowiki>'</nowiki>|#919191}}
{{Text|<nowiki>' This program is distributed in the hope that it will be useful,</nowiki>|#919191}}
{{Text|<nowiki>' but WITHOUT ANY WARRANTY; without even the implied warranty of</nowiki>|#919191}}
{{Text|<nowiki>' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</nowiki>|#919191}}
{{Text|<nowiki>' GNU General Public License for more details.</nowiki>|#919191}}
{{Text|<nowiki>'</nowiki>|#919191}}
{{Text|<nowiki>' You should have received a copy of the GNU General Public License</nowiki>|#919191}}
{{Text|<nowiki>' along with this program; if not, write to the Free Software</nowiki>|#919191}}
{{Text|<nowiki>' Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</nowiki>|#919191}}
{{Text|<nowiki>'--- docs\Base64Encode.html</nowiki>|#919191}}
{{Text|<nowiki>'---------------------------------------------------------------------</nowiki>|#919191}}
{{Cl|FUNCTION}} {{Text|Base64Encode$|#55FF55}} (message$, wrapLen%, padFlag%, lineMode%)
{{Text|<nowiki>'--- option _explicit requirements ---</nowiki>|#919191}}
{{Cl|DIM}} outSize&, numLine&, outBuff$
{{Text|<nowiki>'--- calc output buffer size ---</nowiki>|#919191}}
outSize& = {{Cl|LEN}}(message$) / {{Text|3|#F580B1}} * {{Text|4|#F580B1}} {{Text|<nowiki>'general encoding</nowiki>|#919191}}
outSize& = ((outSize& + {{Text|3|#F580B1}}) {{Cl|AND}} {{Text|-4|#F580B1}}) {{Text|<nowiki>'add padding space</nowiki>|#919191}}
{{Cl|IF}} wrapLen% > {{Text|0|#F580B1}} {{Cl|THEN}}
    numLine& = outSize& / wrapLen%
    outSize& = outSize& + (numLine& * {{Text|2|#F580B1}}) {{Text|<nowiki>'add CR+LF for line wraps</nowiki>|#919191}}
{{Cl|END IF}}
outSize& = outSize& + {{Text|256|#F580B1}} {{Text|<nowiki>'add a nice safety margin</nowiki>|#919191}}
{{Text|<nowiki>'--- base64 encoding ---</nowiki>|#919191}}
outBuff$ = {{Cl|SPACE$}}(outSize&)
outSize& = {{Text|B64Enc&|#55FF55}}(outBuff$, message$, {{Cl|LEN}}(message$), wrapLen%, padFlag%, lineMode%)
{{Text|<nowiki>'--- set result ---</nowiki>|#919191}}
{{Text|Base64Encode$|#55FF55}} = {{Cl|LEFT$}}(outBuff$, outSize&)
{{Cl|END FUNCTION}}
{{Text|<nowiki>'--- docs\Base64Decode.html</nowiki>|#919191}}
{{Text|<nowiki>'---------------------------------------------------------------------</nowiki>|#919191}}
{{Cl|FUNCTION}} {{Text|Base64Decode$|#55FF55}} (encoded$, strict%, lineMode%)
{{Text|<nowiki>'--- option _explicit requirements ---</nowiki>|#919191}}
{{Cl|DIM}} outBuff$, outSize&
{{Text|<nowiki>'--- base64 decoding ---</nowiki>|#919191}}
outBuff$ = {{Cl|SPACE$}}({{Cl|LEN}}(encoded$))
outSize& = {{Text|B64Dec&|#55FF55}}(outBuff$, encoded$, {{Cl|LEN}}(encoded$), strict%, lineMode%)
{{Text|<nowiki>'--- check for errors & set result ---</nowiki>|#919191}}
{{Cl|SELECT CASE}} outSize&
    {{Cl|CASE}} B64DEC_ERROR_DAP
        {{Text|Base64Decode$|#55FF55}} = {{Text|<nowiki>"Base64-decoding-Error: Data after pad character."</nowiki>|#FFB100}}
    {{Cl|CASE}} B64DEC_ERROR_PCW
        {{Text|Base64Decode$|#55FF55}} = {{Text|<nowiki>"Base64-decoding-Error: Pad character in wrong place."</nowiki>|#FFB100}}
    {{Cl|CASE}} B64DEC_ERROR_SSB
        {{Text|Base64Decode$|#55FF55}} = {{Text|<nowiki>"Base64-decoding-Error: Single symbol block not valid."</nowiki>|#FFB100}}
    {{Cl|CASE}} B64DEC_ERROR_BCI
        {{Text|Base64Decode$|#55FF55}} = {{Text|<nowiki>"Base64-decoding-Error: Bad character in input string."</nowiki>|#FFB100}}
    {{Cl|CASE}} {{Cl|ELSE}}
        {{Text|Base64Decode$|#55FF55}} = {{Cl|LEFT$}}(outBuff$, outSize&)
{{Cl|END SELECT}}
{{Cl|END FUNCTION}}
{{Text|<nowiki>'--- undocumented - for internal use only</nowiki>|#919191}}
{{Text|<nowiki>'---------------------------------------------------------------------</nowiki>|#919191}}
{{Cl|FUNCTION}} {{Text|VersionBase64$|#55FF55}}
{{Text|VersionBase64$|#55FF55}} = {{Cl|MID$ (function)|MID$}}({{Text|<nowiki>"$VER: base64.bm 1.2 (06-Feb-2022) by RhoSigma :END$"</nowiki>|#FFB100}}, {{Text|7|#F580B1}}, {{Text|39|#F580B1}})
{{Cl|END FUNCTION}}
{{CodeEnd}}

Revision as of 10:22, 31 May 2024

ATTENTION

Keep Out - This is my all things MediaWiki Nuke Test Site ...

base64.h

//this file is part of MimeTools (plugin for Notepad++)
//Copyright (C)2019 Don HO <don.h@free.fr>
//
//
// Enhance Base64 features, and rewrite Base64 encode/decode implementation
// Copyright 2019 by Paul Nankervis <paulnank@hotmail.com>
//
// Added "RSQB" include guard and namespace for use in QB64, Feb-5th-2022
// Done by RhoSigma, Roland Heyder <support@rhosigma-cw.net>
//
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


// Base64 encoding decoding - where 8 bit ascii is re-represented using just 64 ascii characters (plus optional padding '=').
//
// This code includes options to encode to base64 in multiple ways. For example the text lines:-
//
//  If you can keep your head when all about you
//  Are losing theirs and blaming it on you;
//
// Using "Encode with Unix EOL" would produce a single base64 string with line breaks after each 64 characters:-
//
//  SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3UNCkFy
//  ZSBsb3NpbmcgdGhlaXJzIGFuZCBibGFtaW5nIGl0IG9uIHlvdTs=
//
// That would be decoded using a single base64 decode which ignored whitespace characters (the line breaks).
//
// Alternatively the same lines could be encoded using a "by line" option to encode each line of input as
// its own separate base64 string:-
//
//  SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3U
//  QXJlIGxvc2luZyB0aGVpcnMgYW5kIGJsYW1pbmcgaXQgb24geW91Ow
//
// Each of these output lines could be decoded separately, or multiple lines decoded using "reset on whitespace"
// to cause base64 decoding to restart on each line


#ifndef RSQB_BASE64_H // include only once
#define RSQB_BASE64_H

namespace rsqbbase64 {

char base64CharSet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int base64CharMap[] = {  // base64 values or: -1 for illegal character, -2 to ignore character, and -3 for pad ('=')
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,  // <tab> <lf> & <cr> are ignored
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,  // <space> is ignored
    52, 53, 54, 55 ,56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,  // '=' is the pad character
    -1,  0,  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, -1, -1, -1, -1 ,-1,
    -1, 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, -1, -1, -1, -1, -1
};

// base64Encode simply converts ascii to base64 with appropriate wrapping and padding. Encoding is done by loading
// three ascii characters at a time into a bitField, and then extracting them as four base64 values.
// returnString is assumed to be large enough to contain the result (which is typically 4 / 3 the input size
// plus line breaks), and the function return is the length of the result
// wrapLength sets the length at which to wrap the encoded test at (not valid with byLineFlag)
// padFlag controls whether the one or two '=' pad characters are included at the end of encoding
// byLineFlag causes each input line to be encoded as a separate base64 string

int base64Encode(char *resultString, const char *asciiString, size_t asciiStringLength, size_t wrapLength, bool padFlag, bool byLineFlag)
{
    size_t index; // input string index
    size_t lineLength = 0; // current line length
    int resultLength = 0, // result string length
        bitField, // assembled bit field (up to 3 ascii characters at a time)
        bitOffset = -1, // offset into bit field (8 bit input: 16, 8, 0 -> 6 bit output: 18, 12, 6, 0)
        endOffset, // end offset index value
        charValue; // character value

    for (index = 0; index < asciiStringLength; )
    {
        bitField = 0;
        for (bitOffset = 16; bitOffset >= 0 && index < asciiStringLength; bitOffset -= 8)
        {
            charValue = (UCHAR)asciiString[index];
            if (byLineFlag && (charValue == '\n' || charValue == '\r'))
            {
                break;
            }
            index++;
            bitField |= charValue << bitOffset;
        }
        endOffset = bitOffset + 3; // end indicator
        for (bitOffset = 18; bitOffset > endOffset; bitOffset -= 6)
        {
            if (wrapLength > 0 && lineLength++ >= wrapLength && !byLineFlag)
            {
                resultString[resultLength++] = '\n';
                lineLength = 1;
            }
            resultString[resultLength++] = base64CharSet[(bitField >> bitOffset) & 0x3f];
        }
        if (byLineFlag)
        {
            while (index < asciiStringLength && (asciiString[index] == '\n' || asciiString[index] == '\r'))
            {
                resultString[resultLength++] = asciiString[index++];
            }
        }
    }
    if (padFlag && !byLineFlag)
    {
        for (; bitOffset >= 0; bitOffset -= 6)
        {
            if (wrapLength > 0 && lineLength++ >= wrapLength)
            {
                resultString[resultLength++] = '\n';
                lineLength = 1;
            }
            resultString[resultLength++] = '=';
        }
    }
    return resultLength;
}

// base64Decode converts base64 to ascii. But there are choices about what to do with illegal characters or
// malformed strings. In this version there is a strict flag to indicate that the input must be a single
// valid base64 string with no illegal characters, no extra padding, and no short segments. Otherwise
// there is best effort to decode around illegal characters which ARE preserved in the output.
// So  "TWFyeQ==.aGFk.YQ.bGl0dGxl.bGFtYg=="  decodes to  "Mary.had.a.little.lamb"  with five seperate
// base64 strings decoded, each separated by the illegal character dot. In strict mode the first dot
// would trigger a fatal error. Some other implementations choose to ignore illegal characters which
// of course has it's own issues.
// The four whitespace characters <CR> <LF> <TAB> and <SPACE> are silently ignored unless noWhitespaceFlag
// is set. In this case whitespace is treated similar to illegal characters and base64 decoding operates
// around the white space. So "TWFyeQ== aGFk YQ bGl0dGxl bGFtYg==" would decode as "Mary had a little lamb".
// Decoding is done by loading four base64 characters at a time into a bitField, and then extracting them as
// three ascii characters.
// returnString is assumed to be large enough to contain the result (which could be the same size as the input),
// and the function return is the length of the result, or a negative value in case of an error

int base64Decode(char *resultString, const char *encodedString, size_t encodedStringLength, bool strictFlag, bool whitespaceReset)
{
    size_t index; // input string index

    int resultLength = 0, // result string length
        bitField, // assembled bit field (up to 3 ascii characters at a time)
        bitOffset, // offset into bit field (6 bit intput: 18, 12, 6, 0 -> 8 bit output: 16, 8, 0)
        endOffset, // end offset index value
        charValue = 0, // character value
        charIndex = 0, // character index
        padLength = 0; // pad characters seen

    for (index = 0; index < encodedStringLength; )
    {
        bitField = 0;
        for (bitOffset = 18; bitOffset >= 0 && index < encodedStringLength; )
        {
            charValue = (UCHAR)encodedString[index++];
            charIndex = base64CharMap[charValue & 0x7f];
            if (charIndex >= 0)
            {
                if (padLength > 0 && strictFlag)
                {
                    return -1; // **ERROR** Data after pad character
                }
                bitField |= charIndex << bitOffset;
                bitOffset -= 6;
            }
            else
            {
                if (charIndex == -3) // -3 is Pad character '='
                {
                    padLength++;
                    if (strictFlag && bitOffset > 6)
                    {
                        return -2; // **ERROR** Pad character in wrong place
                    }
                }
                else // either -1 for illegal character or -2 for whitespace (ignored)
                {
                    if (charIndex == -1 || whitespaceReset)
                    {
                        charIndex = -1; // Remember it as an illegal character for copy below
                        break;  // exit loop to deal with illegal character
                    }
                }
            }
        }

        if (strictFlag && bitOffset == 12)
        {
            return -3; // **ERROR** Single symbol block not valid
        }
        endOffset = bitOffset + 3; // end indicator

        for (bitOffset = 16; bitOffset > endOffset; bitOffset -= 8)
        {
            resultString[resultLength++] = (bitField >> bitOffset) & 0xff;
        }

        if (charIndex == -1) // Was there an illegal character?
        {
            if (strictFlag)
            {
                return -4; // **ERROR** Bad character in input string
            }
            resultString[resultLength++] = (char)charValue;
        }
    }
    return resultLength;
}

} // namespace

#endif // RSQB_BASE64_H

base64.bi

'+---------------+---------------------------------------------------+
'| ###### ###### |     .--. .         .-.                            |
'| ##  ## ##   # |     |   )|        (   ) o                         |
'| ##  ##  ##    |     |--' |--. .-.  `-.  .  .-...--.--. .-.        |
'| ######   ##   |     |  \ |  |(   )(   ) | (   ||  |  |(   )       |
'| ##      ##    |     '   `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |
'| ##     ##   # |                            ._.'                   |
'| ##     ###### |   Sources & Documents placed under GNU GPL v2.0   |
'+---------------+---------------------------------------------------+
'|                                                                   |
'| === base64.bi ===                                                 |
'|                                                                   |
'| == Definitions required for the routines provided in base64.bm.   |
'|                                                                   |
'+-------------------------------------------------------------------+
'| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |
'| Find me in the QB64 Forum or mail to support@rhosigma-cw.net for  |
'| any questions or suggestions. Thanx for your interest in my work. |
'+-------------------------------------------------------------------+

' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either
' version 2 of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

'--- Helper functions defined in base64.h, which should reside (along with
'--- the respective .bi/.bm files) in the main QB64 installation folder.
'-----
'--- If you rather place your library files in a sub-directory (as I do),
'--- then you must specify the path within the DECLARE LIBRARY statement
'--- assuming the main QB64 installation folder as root.
'-----
DECLARE LIBRARY "QB64Library\B64-Coding\base64" 'Do not add .h here !!
    FUNCTION B64Enc& ALIAS "rsqbbase64::base64Encode" (resultString$, asciiString$, BYVAL asciiStringLength&, BYVAL wrapLength&, BYVAL padFlag&, BYVAL byLineFlag&)
    FUNCTION B64Dec& ALIAS "rsqbbase64::base64Decode" (resultString$, encodedString$, BYVAL encodedStringLength&, BYVAL strictFlag&, BYVAL whitespaceReset&)
    'The low level wrappers to Base64, you should not use this directly,
    'rather use the QB64 FUNCTIONs provided in the base64.bm include.
END DECLARE

'--- Possible Base64 decoding errors
'-----
CONST B64DEC_ERROR_DAP = -1 'Data after pad character
CONST B64DEC_ERROR_PCW = -2 'Pad character in wrong place
CONST B64DEC_ERROR_SSB = -3 'Single symbol block not valid
CONST B64DEC_ERROR_BCI = -4 'Bad character in input string

base64.bm

'+---------------+---------------------------------------------------+
'| ###### ###### |     .--. .         .-.                            |
'| ##  ## ##   # |     |   )|        (   ) o                         |
'| ##  ##  ##    |     |--' |--. .-.  `-.  .  .-...--.--. .-.        |
'| ######   ##   |     |  \ |  |(   )(   ) | (   ||  |  |(   )       |
'| ##      ##    |     '   `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |
'| ##     ##   # |                            ._.'                   |
'| ##     ###### |   Sources & Documents placed under GNU GPL v2.0   |
'+---------------+---------------------------------------------------+
'|                                                                   |
'| === base64.bm ===                                                 |
'|                                                                   |
'| == The library provides FUNCTIONs for Base64 en-/decoding in QB64.|
'| == Encoding and decoding is done from string to string.           |
'|                                                                   |
'+-------------------------------------------------------------------+
'| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |
'| Find me in the QB64 Forum or mail to support@rhosigma-cw.net for  |
'| any questions or suggestions. Thanx for your interest in my work. |
'+-------------------------------------------------------------------+

' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either
' version 2 of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

'--- docs\Base64Encode.html
'---------------------------------------------------------------------
FUNCTION Base64Encode$ (message$, wrapLen%, padFlag%, lineMode%)
'--- option _explicit requirements ---
DIM outSize&, numLine&, outBuff$
'--- calc output buffer size ---
outSize& = LEN(message$) / 3 * 4 'general encoding
outSize& = ((outSize& + 3) AND -4) 'add padding space
IF wrapLen% > 0 THEN
    numLine& = outSize& / wrapLen%
    outSize& = outSize& + (numLine& * 2) 'add CR+LF for line wraps
END IF
outSize& = outSize& + 256 'add a nice safety margin
'--- base64 encoding ---
outBuff$ = SPACE$(outSize&)
outSize& = B64Enc&(outBuff$, message$, LEN(message$), wrapLen%, padFlag%, lineMode%)
'--- set result ---
Base64Encode$ = LEFT$(outBuff$, outSize&)
END FUNCTION

'--- docs\Base64Decode.html
'---------------------------------------------------------------------
FUNCTION Base64Decode$ (encoded$, strict%, lineMode%)
'--- option _explicit requirements ---
DIM outBuff$, outSize&
'--- base64 decoding ---
outBuff$ = SPACE$(LEN(encoded$))
outSize& = B64Dec&(outBuff$, encoded$, LEN(encoded$), strict%, lineMode%)
'--- check for errors & set result ---
SELECT CASE outSize&
    CASE B64DEC_ERROR_DAP
        Base64Decode$ = "Base64-decoding-Error: Data after pad character."
    CASE B64DEC_ERROR_PCW
        Base64Decode$ = "Base64-decoding-Error: Pad character in wrong place."
    CASE B64DEC_ERROR_SSB
        Base64Decode$ = "Base64-decoding-Error: Single symbol block not valid."
    CASE B64DEC_ERROR_BCI
        Base64Decode$ = "Base64-decoding-Error: Bad character in input string."
    CASE ELSE
        Base64Decode$ = LEFT$(outBuff$, outSize&)
END SELECT
END FUNCTION

'--- undocumented - for internal use only
'---------------------------------------------------------------------
FUNCTION VersionBase64$
VersionBase64$ = MID$("$VER: base64.bm 1.2 (06-Feb-2022) by RhoSigma :END$", 7, 39)
END FUNCTION