﻿{
  CSCoder.LGPD
  Claudio Santos | CS Coder | https://cscoder.com.br/
  SPDX-License-Identifier: MIT

  Uso, modificacao e redistribuicao permitidos, inclusive em projetos
  comerciais. Preserve o aviso de autoria e o texto da licenca.
  Fornecido sem garantias, conforme a licenca abaixo.

  MIT License
  
  Copyright (c) 2026 Claudio Santos (CS Coder)
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
}

unit CSCoder.LGPD;

interface

type
  TPosicaoVisivel = (pvInicio, pvMeio, pvFim);

// Mascaramento de exibicao; nao e anonimizacao nem validacao de DV.
function OcultarCPF(const Valor: string;
  QuantidadeVisivel: Integer = 2;
  Posicao: TPosicaoVisivel = pvMeio): string;
function OcultarCNPJ(const Valor: string;
  QuantidadeVisivel: Integer = 2;
  Posicao: TPosicaoVisivel = pvMeio): string;

implementation

uses System.SysUtils;

function NormalizarCPF(const Valor: string; out Documento: string): Boolean;
var
  S: string;
  I: Integer;
begin
  Result := False;
  Documento := '';
  S := Trim(Valor);
  case Length(S) of
    11: Documento := S;
    14:
      begin
        if (S[4] <> '.') or (S[8] <> '.') or (S[12] <> '-') then
          Exit;
        Documento := Copy(S, 1, 3) + Copy(S, 5, 3) +
                     Copy(S, 9, 3) + Copy(S, 13, 2);
      end;
  else
    Exit;
  end;
  for I := 1 to 11 do
    if (Documento[I] < '0') or (Documento[I] > '9') then
      Exit;
  Result := True;
end;

function NormalizarCNPJ(const Valor: string; out Documento: string): Boolean;
var
  S: string;
  I: Integer;
  C: Char;
begin
  Result := False;
  Documento := '';
  S := Trim(Valor);
  case Length(S) of
    14: Documento := S;
    18:
      begin
        if (S[3] <> '.') or (S[7] <> '.') or
           (S[11] <> '/') or (S[16] <> '-') then
          Exit;
        Documento := Copy(S, 1, 2) + Copy(S, 4, 3) +
                     Copy(S, 8, 3) + Copy(S, 12, 4) + Copy(S, 17, 2);
      end;
  else
    Exit;
  end;
  for I := 1 to 12 do
  begin
    C := Documento[I];
    if (C >= 'a') and (C <= 'z') then
      C := Char(Ord(C) - Ord('a') + Ord('A'));
    if not (((C >= '0') and (C <= '9')) or
            ((C >= 'A') and (C <= 'Z'))) then
      Exit;
    Documento[I] := C;
  end;
  for I := 13 to 14 do
    if (Documento[I] < '0') or (Documento[I] > '9') then
      Exit;
  Result := True;
end;

function AplicarMascara(const Documento, Modelo: string;
  QuantidadeVisivel: Integer; Posicao: TPosicaoVisivel): string;
var
  Inicio, I, PosicaoDocumento: Integer;
begin
  Result := Modelo;
  if (QuantidadeVisivel < 1) or (QuantidadeVisivel > 2) then
    Exit;
  case Posicao of
    pvInicio: Inicio := 1;
    pvMeio: Inicio := (Length(Documento) - QuantidadeVisivel) div 2 + 1;
    pvFim: Inicio := Length(Documento) - QuantidadeVisivel + 1;
  else
    Exit;
  end;
  PosicaoDocumento := 0;
  for I := 1 to Length(Result) do
    if Modelo[I] = '*' then
    begin
      Inc(PosicaoDocumento);
      if (PosicaoDocumento >= Inicio) and
         (PosicaoDocumento < Inicio + QuantidadeVisivel) then
        Result[I] := Documento[PosicaoDocumento];
    end;
end;

function OcultarCPF(const Valor: string;
  QuantidadeVisivel: Integer; Posicao: TPosicaoVisivel): string;
var
  Documento: string;
begin
  Result := '***.***.***-**';
  if (QuantidadeVisivel < 1) or (QuantidadeVisivel > 2) then
    Exit;
  if NormalizarCPF(Valor, Documento) then
    Result := AplicarMascara(Documento, Result, QuantidadeVisivel, Posicao);
end;

function OcultarCNPJ(const Valor: string;
  QuantidadeVisivel: Integer; Posicao: TPosicaoVisivel): string;
var
  Documento: string;
begin
  Result := '**.***.***/****-**';
  if (QuantidadeVisivel < 1) or (QuantidadeVisivel > 2) then
    Exit;
  if NormalizarCNPJ(Valor, Documento) then
    Result := AplicarMascara(Documento, Result, QuantidadeVisivel, Posicao);
end;

end.
