Function ChkCpf(cic)
if !vcpf(cic)
   if !vcgc(cic)
      return .f.
   else
      return .t.
   endif
else
   return .t.
endif

FUNCTION VCPF(cpf)
LOCAL i,ii,soma,controle,digito,num
cpf = IF(VALTYPE(cpf)="U","",cpf)
IF EMPT(cpf)
    RETURN(.T.)
ELSE
    IF LEN(cpf) < 10
        RETURN(.F.)
    ENDIF
ENDIF
controle=""
num=10
FOR ii=1 TO 2
    soma=0
    FOR i=1 TO 9
        soma+=(VAL(SUBSTR(cpf,i,1))*num)
        num--
    NEXT
    IF ii=2
        soma+=(2*VAL(controle))
    ENDIF
    digito=soma - (INT(soma/11)*11)
    IF digito=0 .OR. digito=1
        controle=controle+"0"
    ELSE
        controle=controle+STR(11-digito,1)
    ENDIF
    num=11
NEXT
IF controle!=SUBST(cpf,10,2)
    RETURN(.F.)
ENDIF
RETURN(.T.)

FUNCTION VCGC(cgc)
LOCAL i,ii,soma,controle,digito,num
cgc = IF(VALTYPE(cgc)="U","",cgc)
IF EMPT(cgc)
    RETURN(.T.)
ELSE
    IF LEN(cgc) < 14
        RETURN(.F.)
    ENDIF
ENDIF
controle=""
num=5
FOR ii=1 TO 2
    soma=0
    FOR i=1 TO 12
        soma+=(VAL(SUBSTR(cgc,i,1))*num)
        num--
        IF num=1
            num=9
        ENDIF
    NEXT
    IF ii=2
        soma+=(2*VAL(controle))
    ENDIF
    digito=soma - (INT(soma/11)*11)
    IF digito=0 .OR. digito=1
        controle=controle+"0"
    ELSE
        controle=controle+STR(11-digito,1)
    ENDIF
    num=6
NEXT
IF controle!=SUBST(cgc,13,2)
    RETURN(.F.)
ENDIF
RETURN(.T.)