From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: redhat.com, ip: 209.132.183.28, mailfrom: lersek@redhat.com) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by groups.io with SMTP; Tue, 02 Jul 2019 03:29:06 -0700 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7CE2730872ED; Tue, 2 Jul 2019 10:28:55 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-117-172.ams2.redhat.com [10.36.117.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2652419730; Tue, 2 Jul 2019 10:28:50 +0000 (UTC) From: "Laszlo Ersek" To: edk2-devel-groups-io Cc: Liming Gao , =?UTF-8?q?Marvin=20H=C3=A4user?= , Michael D Kinney , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Zhichao Gao Subject: [PATCH 2/3] MdePkg/BaseLib: rewrite Base64Decode() Date: Tue, 2 Jul 2019 12:28:35 +0200 Message-Id: <20190702102836.27589-3-lersek@redhat.com> In-Reply-To: <20190702102836.27589-1-lersek@redhat.com> References: <20190702102836.27589-1-lersek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Tue, 02 Jul 2019 10:29:03 +0000 (UTC) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Rewrite Base64Decode() from scratch, due to reasons listed in the second reference below. Implement Base64Decode() according to the specification added in the previous patch. The decoder scans the input buffer once, it has no inner loop(s), and it spills each output byte as soon as the output byte is complete. Cc: Liming Gao Cc: Marvin H=C3=A4user Cc: Michael D Kinney Cc: Philippe Mathieu-Daud=C3=A9 Cc: Zhichao Gao Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1891 Ref: http://mid.mail-archive.com/c495bd0b-ea4d-7206-8a4f-a7149760d19a@red= hat.com Signed-off-by: Laszlo Ersek --- MdePkg/Library/BaseLib/String.c | 249 +++++++++++++++++++- 1 file changed, 247 insertions(+), 2 deletions(-) diff --git a/MdePkg/Library/BaseLib/String.c b/MdePkg/Library/BaseLib/Str= ing.c index f8397035c32a..6198ccbc9672 100644 --- a/MdePkg/Library/BaseLib/String.c +++ b/MdePkg/Library/BaseLib/String.c @@ -1973,8 +1973,253 @@ Base64Decode ( IN OUT UINTN *DestinationSize ) { - ASSERT (FALSE); - return RETURN_INVALID_PARAMETER; + BOOLEAN PaddingMode; + UINTN SixBitGroupsConsumed; + UINT32 Accumulator; + UINTN OriginalDestinationSize; + UINTN SourceIndex; + + if (DestinationSize =3D=3D NULL) { + return RETURN_INVALID_PARAMETER; + } + + // + // Check Source array validity. + // + if (Source =3D=3D NULL) { + if (SourceSize > 0) { + // + // At least one CHAR8 element at NULL Source. + // + return RETURN_INVALID_PARAMETER; + } + } else if (SourceSize > MAX_ADDRESS - (UINTN)Source) { + // + // Non-NULL Source, but it wraps around. + // + return RETURN_INVALID_PARAMETER; + } + + // + // Check Destination array validity. + // + if (Destination =3D=3D NULL) { + if (*DestinationSize > 0) { + // + // At least one UINT8 element at NULL Destination. + // + return RETURN_INVALID_PARAMETER; + } + } else if (*DestinationSize > MAX_ADDRESS - (UINTN)Destination) { + // + // Non-NULL Destination, but it wraps around. + // + return RETURN_INVALID_PARAMETER; + } + + // + // Check for overlap. + // + if (Source !=3D NULL && Destination !=3D NULL) { + // + // Both arrays have been provided, and we know from earlier that eac= h array + // is valid in itself. + // + if ((UINTN)Source + SourceSize <=3D (UINTN)Destination) { + // + // Source array precedes Destination array, OK. + // + } else if ((UINTN)Destination + *DestinationSize <=3D (UINTN)Source)= { + // + // Destination array precedes Source array, OK. + // + } else { + // + // Overlap. + // + return RETURN_INVALID_PARAMETER; + } + } + + // + // Decoding loop setup. + // + PaddingMode =3D FALSE; + SixBitGroupsConsumed =3D 0; + Accumulator =3D 0; + OriginalDestinationSize =3D *DestinationSize; + *DestinationSize =3D 0; + + // + // Decoding loop. + // + for (SourceIndex =3D 0; SourceIndex < SourceSize; SourceIndex++) { + CHAR8 SourceChar; + UINT32 Base64Value; + UINT8 DestinationOctet; + + SourceChar =3D Source[SourceIndex]; + + // + // Whitespace is ignored at all positions (regardless of padding mod= e). + // + if (SourceChar =3D=3D '\t' || SourceChar =3D=3D '\n' || SourceChar =3D= =3D '\v' || + SourceChar =3D=3D '\f' || SourceChar =3D=3D '\r' || SourceChar =3D= =3D ' ') { + continue; + } + + // + // If we're in padding mode, accept another padding character, as lo= ng as + // that padding character completes the quantum. This completes case= (2) + // from RFC4648, Chapter 4. "Base 64 Encoding": + // + // (2) The final quantum of encoding input is exactly 8 bits; here, = the + // final unit of encoded output will be two characters followed = by two + // "=3D" padding characters. + // + if (PaddingMode) { + if (SourceChar =3D=3D '=3D' && SixBitGroupsConsumed =3D=3D 3) { + SixBitGroupsConsumed =3D 0; + continue; + } + return RETURN_INVALID_PARAMETER; + } + + // + // When not in padding mode, decode Base64Value based on RFC4648, "T= able 1: + // The Base 64 Alphabet". + // + if ('A' <=3D SourceChar && SourceChar <=3D 'Z') { + Base64Value =3D SourceChar - 'A'; + } else if ('a' <=3D SourceChar && SourceChar <=3D 'z') { + Base64Value =3D 26 + (SourceChar - 'a'); + } else if ('0' <=3D SourceChar && SourceChar <=3D '9') { + Base64Value =3D 52 + (SourceChar - '0'); + } else if (SourceChar =3D=3D '+') { + Base64Value =3D 62; + } else if (SourceChar =3D=3D '/') { + Base64Value =3D 63; + } else if (SourceChar =3D=3D '=3D') { + // + // Enter padding mode. + // + PaddingMode =3D TRUE; + + if (SixBitGroupsConsumed =3D=3D 2) { + // + // If we have consumed two 6-bit groups from the current quantum= before + // encountering the first padding character, then this is case (= 2) from + // RFC4648, Chapter 4. "Base 64 Encoding". Bump SixBitGroupsCons= umed, + // and we'll enforce another padding character. + // + SixBitGroupsConsumed =3D 3; + } else if (SixBitGroupsConsumed =3D=3D 3) { + // + // If we have consumed three 6-bit groups from the current quant= um + // before encountering the first padding character, then this is= case + // (3) from RFC4648, Chapter 4. "Base 64 Encoding". The quantum = is now + // complete. + // + SixBitGroupsConsumed =3D 0; + } else { + // + // Padding characters are not allowed at the first two positions= of a + // quantum. + // + return RETURN_INVALID_PARAMETER; + } + + // + // Wherever in a quantum we enter padding mode, we enforce the pad= ding + // bits pending in the accumulator -- from the last 6-bit group ju= st + // preceding the padding character -- to be zero. Refer to RFC4648= , + // Chapter 3.5. "Canonical Encoding". + // + if (Accumulator !=3D 0) { + return RETURN_INVALID_PARAMETER; + } + + // + // Advance to the next source character. + // + continue; + } else { + // + // Other characters outside of the encoding alphabet are rejected. + // + return RETURN_INVALID_PARAMETER; + } + + // + // Feed the bits of the current 6-bit group of the quantum to the + // accumulator. + // + Accumulator =3D (Accumulator << 6) | Base64Value; + SixBitGroupsConsumed++; + switch (SixBitGroupsConsumed) { + case 1: + // + // No octet to spill after consuming the first 6-bit group of the + // quantum; advance to the next source character. + // + continue; + case 2: + // + // 12 bits accumulated (6 pending + 6 new); prepare for spilling a= n + // octet. 4 bits remain pending. + // + DestinationOctet =3D (UINT8)(Accumulator >> 4); + Accumulator &=3D 0xF; + break; + case 3: + // + // 10 bits accumulated (4 pending + 6 new); prepare for spilling a= n + // octet. 2 bits remain pending. + // + DestinationOctet =3D (UINT8)(Accumulator >> 2); + Accumulator &=3D 0x3; + break; + default: + ASSERT (SixBitGroupsConsumed =3D=3D 4); + // + // 8 bits accumulated (2 pending + 6 new); prepare for spilling an= octet. + // The quantum is complete, 0 bits remain pending. + // + DestinationOctet =3D (UINT8)Accumulator; + Accumulator =3D 0; + SixBitGroupsConsumed =3D 0; + break; + } + + // + // Store the decoded octet if there's room left. Increment + // (*DestinationSize) unconditionally. + // + if (*DestinationSize < OriginalDestinationSize) { + ASSERT (Destination !=3D NULL); + Destination[*DestinationSize] =3D DestinationOctet; + } + (*DestinationSize)++; + + // + // Advance to the next source character. + // + } + + // + // If Source terminates mid-quantum, then Source is invalid. + // + if (SixBitGroupsConsumed !=3D 0) { + return RETURN_INVALID_PARAMETER; + } + + // + // Done. + // + if (*DestinationSize <=3D OriginalDestinationSize) { + return RETURN_SUCCESS; + } + return RETURN_BUFFER_TOO_SMALL; } =20 /** --=20 2.19.1.3.g30247aa5d201