CountUTF8ToUnicode

NTSTATUS __stdcall CountUTF8ToUnicode(PSTR bytes, UINT64 count, UINT64 *pCharCount){
  int v3; 
  char *v4; 
  NTSTATUS v5; 
  unsigned int v7; 
  unsigned int v8; 
  unsigned int v9; 
  unsigned int v10; 
  unsigned __int64 v11; 
  unsigned int v12; 
  char v13; 
  char v14; 
  __int64 v15; 
  int v16; 
  NTSTATUS result; 

  v3 = 0;
  v4 = &bytes[(unsigned int)count];
  v5 = 0;
  while( bytes < v4 )
  {
    v7 = *bytes++;
    if( v3 )
    {
      if( (v7 & 0xC0) != 0x80 )
      {
        --bytes;
        LODWORD(count) = (v3 >> 30) + count;
LABEL_7:
        v5 = 263;
        LODWORD(count) = count + 1;
        goto LABEL_8;
      }
      v3 = (v3 << 6) | v7 & 0x3F;
      if( (v3 & 0x20000000) != 0 )
      {
LABEL_17:
        if( (unsigned __int64)(v4 - bytes) <= 0xD )
        {
          while( bytes < v4 )
          {
            v8 = *bytes++;
            if( v8 > 0x7F )
              goto LABEL_20;
          }
          v3 = 0;
          break;
        }
        v11 = (unsigned __int64)(v4 - 7);
        while( (unsigned __int64)bytes < v11 )
        {
          v12 = *bytes++;
          if( v12 <= 0x7F )
          {
            if( ((unsigned __int8)bytes & 1) == 0 || (v12 = *bytes, ++bytes, v12 <= 0x7F) )
            {
              if( ((unsigned __int8)bytes & 2) == 0 )
                goto LABEL_37;
              LOWORD(v12) = *(_WORD *)bytes;
              if( (*(_WORD *)bytes & 0x8080) == 0 )
              {
LABEL_37:
                for( bytes += 2; (unsigned __int64)bytes < v11; bytes += 8 )
                {
                  v12 = *(_DWORD *)bytes;
                  if( ((*(_DWORD *)bytes | *((_DWORD *)bytes + 1)) & 0x80808080) != 0 )
                    goto LABEL_42;
                  bytes += 8;
                  if( (unsigned __int64)bytes >= v11 )
                    break;
                  v12 = *(_DWORD *)bytes;
                  if( ((*(_DWORD *)bytes | *((_DWORD *)bytes + 1)) & 0x80808080) != 0 )
                    goto LABEL_42;
                }
                break;
              }
LABEL_42:
              ++bytes;
              if( (unsigned __int8)v12 <= 0x7Fu )
                continue;
            }
          }
          v13 = *bytes++;
          if( (v12 & 0x40) == 0 || (v13 & 0xC0) != 0x80 )
          {
LABEL_60:
            bytes -= 2;
            break;
          }
          if( (v12 & 0x20) != 0 )
          {
            if( (v12 & 0x10) != 0 )
            {
              if( ((v13 & 0x3F | ((v12 & 0xF) << 6)) >> 4) - 1 > 0xF )
                goto LABEL_60;
              v14 = *bytes & 0xC0;
              if( v14 != (char)0x80 || (bytes[1] & 0xC0) != v14 )
                goto LABEL_60;
              v15 = 2i64;
            }
            else
            {
              v16 = v13 & 0x20 | ((v12 & 0xF) << 6) & 0x3E0;
              if( !v16 || v16 == 864 || (*bytes & 0xC0) != 0x80 )
                goto LABEL_60;
              v15 = 1i64;
            }
            LODWORD(count) = count - 1;
            bytes += v15;
          }
          else if( (v12 & 0x1E) == 0 )
          {
            goto LABEL_60;
          }
          LODWORD(count) = count - 1;
        }
LABEL_8:
        v3 = 0;
      }
      else if( (v3 & 0x10000000) != 0 )
      {
        if( (v3 & 0x800000) == 0 && (v3 & 0x1F0u) - 16 > 0xF0 )
          goto LABEL_7;
      }
      else if( (v3 & 0x3E0) == 0 || (v3 & 0x3E0) == 864 )
      {
        goto LABEL_7;
      }
    }
    else
    {
      v8 = v7;
LABEL_20:
      if( v8 <= 0x7F )
        goto LABEL_17;
      LODWORD(count) = count - 1;
      if( (v8 & 0x40) == 0 )
        goto LABEL_7;
      if( (v8 & 0x20) != 0 )
      {
        v9 = v8 & 0xF;
        if( (v8 & 0x10) != 0 )
        {
          if( v9 <= 4 )
          {
            v3 = v9 | 0x504D0C00;
            goto LABEL_26;
          }
          goto LABEL_7;
        }
        v3 = v9 | 0x48228000;
LABEL_26:
        LODWORD(count) = count - 1;
      }
      else
      {
        v10 = v8 & 0x1F;
        if( v10 <= 1 )
          goto LABEL_7;
        v3 = v10 | 0x800000;
      }
    }
  }
  if( v3 )
  {
    LODWORD(count) = (v3 >> 30) + count + 1;
    v5 = 263;
  }
  result = v5;
  *(_DWORD *)pCharCount = 2 * count;
  return result;
}

Referenced by:

RtlUTF8StringToUnicodeString
RtlUTF8ToUnicodeN