Fix IP address text representation issue about leading zeros

1.It keeps the rule that Leading zero’s compression(Yes/Not) need to be consistent throughout the whole IP address.
2.It also fixes some issue to recognize some invalid representation.


git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@10777 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
jjin9 2010-08-06 08:31:00 +00:00
parent 1a2ae0f71f
commit 7754556334
1 changed files with 56 additions and 3 deletions

View File

@ -2808,13 +2808,17 @@ NetLibAsciiStrToIp6 (
UINTN NodeVal;
BOOLEAN Short;
BOOLEAN Update;
BOOLEAN LeadZero;
UINT8 LeadZeroCnt;
UINT8 Cnt;
if ((String == NULL) || (Ip6Address == NULL)) {
return EFI_INVALID_PARAMETER;
}
Ip6Str = (CHAR8 *) String;
AllowedCnt = 6;
Ip6Str = (CHAR8 *) String;
AllowedCnt = 6;
LeadZeroCnt = 0;
//
// An IPv6 address leading with : looks strange.
@ -2833,6 +2837,7 @@ NetLibAsciiStrToIp6 (
TailNodeCnt = 0;
Short = FALSE;
Update = FALSE;
LeadZero = FALSE;
for (Index = 0; Index < 15; Index = (UINT8) (Index + 2)) {
TempStr = Ip6Str;
@ -2847,12 +2852,17 @@ NetLibAsciiStrToIp6 (
if (*Ip6Str == ':') {
if (*(Ip6Str + 1) == ':') {
if ((*(Ip6Str + 2) == '0') || (NodeCnt > 6)) {
if ((NodeCnt > 6) ||
((*(Ip6Str + 2) != '\0') && (AsciiStrHexToUintn (Ip6Str + 2) == 0))) {
//
// ::0 looks strange. report error to user.
//
return EFI_INVALID_PARAMETER;
}
if ((NodeCnt == 6) && (*(Ip6Str + 2) != '\0') &&
(AsciiStrHexToUintn (Ip6Str + 2) != 0)) {
return EFI_INVALID_PARAMETER;
}
//
// Skip the abbreviation part of IPv6 address.
@ -2884,6 +2894,9 @@ NetLibAsciiStrToIp6 (
Ip6Str = Ip6Str + 2;
} else {
if (*(Ip6Str + 1) == '\0') {
return EFI_INVALID_PARAMETER;
}
Ip6Str++;
NodeCnt++;
if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) {
@ -2903,6 +2916,46 @@ NetLibAsciiStrToIp6 (
if ((NodeVal > 0xFFFF) || (Index > 14)) {
return EFI_INVALID_PARAMETER;
}
if (NodeVal != 0) {
if ((*TempStr == '0') &&
((*(TempStr + 2) == ':') || (*(TempStr + 3) == ':') ||
(*(TempStr + 2) == '\0') || (*(TempStr + 3) == '\0'))) {
return EFI_INVALID_PARAMETER;
}
if ((*TempStr == '0') && (*(TempStr + 4) != '\0') &&
(*(TempStr + 4) != ':')) {
return EFI_INVALID_PARAMETER;
}
} else {
if (((*TempStr == '0') && (*(TempStr + 1) == '0') &&
((*(TempStr + 2) == ':') || (*(TempStr + 2) == '\0'))) ||
((*TempStr == '0') && (*(TempStr + 1) == '0') && (*(TempStr + 2) == '0') &&
((*(TempStr + 3) == ':') || (*(TempStr + 3) == '\0')))) {
return EFI_INVALID_PARAMETER;
}
}
Cnt = 0;
while ((TempStr[Cnt] != ':') && (TempStr[Cnt] != '\0')) {
Cnt++;
}
if (LeadZeroCnt == 0) {
if ((Cnt == 4) && (*TempStr == '0')) {
LeadZero = TRUE;
LeadZeroCnt++;
}
if ((Cnt != 0) && (Cnt < 4)) {
LeadZero = FALSE;
LeadZeroCnt++;
}
} else {
if ((Cnt == 4) && (*TempStr == '0') && (LeadZero == FALSE)) {
return EFI_INVALID_PARAMETER;
}
if ((Cnt != 0) && (Cnt < 4) && (LeadZero == TRUE)) {
return EFI_INVALID_PARAMETER;
}
}
Ip6Address->Addr[Index] = (UINT8) (NodeVal >> 8);
Ip6Address->Addr[Index + 1] = (UINT8) (NodeVal & 0xFF);