/** Add new section entry and entry value into Section head. @param[in] Buffer Section entry data buffer. @param[in] BufferSize Size of section entry. @param[in, out] SectionHead Section item head entry. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated. @retval EFI_SUCCESS Section entry is added. @retval EFI_NOT_FOUND Section entry is not found. @retval EFI_INVALID_PARAMETER Section entry is invalid. **/ EFI_STATUS ProfileGetEntry ( IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT SECTION_ITEM **SectionHead ) { EFI_STATUS Status; SECTION_ITEM *SectionItem; SECTION_ITEM *PtrSection; UINTN Length; UINT8 *PtrBuf; UINT8 *PtrEnd; Status = EFI_SUCCESS; PtrBuf = Buffer; PtrEnd = (UINT8 *) ((UINTN)Buffer + BufferSize - 1); // // First search for '=' // while (PtrBuf <= PtrEnd) { if (*PtrBuf == '=') { break; } PtrBuf++; } if (PtrBuf > PtrEnd) { // // Not found. Invalid line // return EFI_NOT_FOUND; } if (PtrBuf <= Buffer) { // Empty name return EFI_NOT_FOUND; } // // excluding the tailing '=' // Length = PtrBuf - Buffer; ProfileTrim ( Buffer, &Length ); // // Invalid line if the entry name is null // if (Length == 0) { return EFI_NOT_FOUND; } if (!IsValidName((CHAR8 *)Buffer, Length)) { return EFI_INVALID_PARAMETER; } // // Omit this line if no section header has been found before // if (*SectionHead == NULL) { return Status; } PtrSection = *SectionHead; SectionItem = AllocatePool (sizeof (SECTION_ITEM)); if (SectionItem == NULL) { return EFI_OUT_OF_RESOURCES; } SectionItem->PtrSection = NULL; SectionItem->PtrEntry = NULL; SectionItem->PtrValue = NULL; SectionItem->SecNameLen = PtrSection->SecNameLen; SectionItem->PtrNext = *SectionHead; *SectionHead = SectionItem; // // SectionName, add a trailing '\0' // SectionItem->PtrSection = AllocatePool (PtrSection->SecNameLen + 1); if (SectionItem->PtrSection == NULL) { return EFI_OUT_OF_RESOURCES; } CopyMem (SectionItem->PtrSection, PtrSection->PtrSection, PtrSection->SecNameLen + 1); // // EntryName, add a trailing '\0' // SectionItem->PtrEntry = AllocatePool (Length + 1); if (SectionItem->PtrEntry == NULL) { FreePool(SectionItem->PtrSection); return EFI_OUT_OF_RESOURCES; } CopyMem (SectionItem->PtrEntry, Buffer, Length); *(SectionItem->PtrEntry + Length) = '\0'; // // Next search for '#' or ';' // PtrBuf = PtrBuf + 1; Buffer = PtrBuf; while (PtrBuf <= PtrEnd) { if (*PtrBuf == '#' || *PtrBuf == ';') { break; } PtrBuf++; } if (PtrBuf <= Buffer) { // Empty name FreePool(SectionItem->PtrEntry); FreePool(SectionItem->PtrSection); return EFI_NOT_FOUND; } Length = PtrBuf - Buffer; ProfileTrim ( Buffer, &Length ); // // Invalid line if the entry value is null // if (Length == 0) { FreePool(SectionItem->PtrEntry); FreePool(SectionItem->PtrSection); return EFI_NOT_FOUND; } if (!IsValidValue((CHAR8 *)Buffer, Length)) { FreePool(SectionItem->PtrEntry); FreePool(SectionItem->PtrSection); return EFI_INVALID_PARAMETER; } // // EntryValue, add a trailing '\0' // SectionItem->PtrValue = AllocatePool (Length + 1); if (SectionItem->PtrValue == NULL) { FreePool(SectionItem->PtrEntry); FreePool(SectionItem->PtrSection); return EFI_OUT_OF_RESOURCES; } CopyMem (SectionItem->PtrValue, Buffer, Length); *(SectionItem->PtrValue + Length) = '\0'; return EFI_SUCCESS; }
/** Pre process config data buffer into Section entry list and Comment entry list. @param[in] DataBuffer Config raw file buffer. @param[in] BufferSize Size of raw buffer. @param[in, out] SectionHead Pointer to the section entry list. @param[in, out] CommentHead Pointer to the comment entry list. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated. @retval EFI_SUCCESS Config data buffer is preprocessed. @retval EFI_NOT_FOUND Config data buffer is invalid, because Section or Entry is not found. @retval EFI_INVALID_PARAMETER Config data buffer is invalid, because Section or Entry is invalid. **/ EFI_STATUS PreProcessDataFile ( IN UINT8 *DataBuffer, IN UINTN BufferSize, IN OUT SECTION_ITEM **SectionHead, IN OUT COMMENT_LINE **CommentHead ) { EFI_STATUS Status; CHAR8 *Source; CHAR8 *CurrentPtr; CHAR8 *BufferEnd; CHAR8 *PtrLine; UINTN LineLength; UINTN SourceLength; UINTN MaxLineLength; *SectionHead = NULL; *CommentHead = NULL; BufferEnd = (CHAR8 *) ( (UINTN) DataBuffer + BufferSize); CurrentPtr = (CHAR8 *) DataBuffer; MaxLineLength = MAX_LINE_LENGTH; Status = EFI_SUCCESS; PtrLine = AllocatePool (MaxLineLength); if (PtrLine == NULL) { return EFI_OUT_OF_RESOURCES; } while (CurrentPtr < BufferEnd) { Source = CurrentPtr; SourceLength = (UINTN)BufferEnd - (UINTN)CurrentPtr; LineLength = MaxLineLength; // // With the assumption that line length is less than 512 // characters. Otherwise BUFFER_TOO_SMALL will be returned. // Status = ProfileGetLine ( (UINT8 *) Source, SourceLength, (UINT8 *) PtrLine, &LineLength ); if (EFI_ERROR (Status)) { if (Status == EFI_BUFFER_TOO_SMALL) { // // If buffer too small, re-allocate the buffer according // to the returned LineLength and try again. // FreePool (PtrLine); PtrLine = NULL; PtrLine = AllocatePool (LineLength); if (PtrLine == NULL) { Status = EFI_OUT_OF_RESOURCES; break; } SourceLength = LineLength; Status = ProfileGetLine ( (UINT8 *) Source, SourceLength, (UINT8 *) PtrLine, &LineLength ); if (EFI_ERROR (Status)) { break; } MaxLineLength = LineLength; } else { break; } } CurrentPtr = (CHAR8 *) ( (UINTN) CurrentPtr + LineLength); // // Line got. Trim the line before processing it. // ProfileTrim ( (UINT8 *) PtrLine, &LineLength ); // // Blank line // if (LineLength == 0) { continue; } if (PtrLine[0] == '#' || PtrLine[0] == ';') { Status = ProfileGetComments ( (UINT8 *) PtrLine, LineLength, CommentHead ); } else if (PtrLine[0] == '[') { Status = ProfileGetSection ( (UINT8 *) PtrLine, LineLength, SectionHead ); } else { Status = ProfileGetEntry ( (UINT8 *) PtrLine, LineLength, SectionHead ); } if (EFI_ERROR (Status)) { break; } } // // Free buffer // FreePool (PtrLine); return Status; }
/** Add new section item into Section head. @param[in] Buffer Section item data buffer. @param[in] BufferSize Size of section item. @param[in, out] SectionHead Section item head entry. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated. @retval EFI_SUCCESS Section item is NULL or Section item is added. **/ EFI_STATUS ProfileGetSection ( IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT SECTION_ITEM **SectionHead ) { SECTION_ITEM *SectionItem; UINTN Length; UINT8 *PtrBuf; UINT8 *PtrEnd; ASSERT(BufferSize >= 1); // // The first character of Buffer is '[', now we want for ']' // PtrEnd = (UINT8 *)((UINTN)Buffer + BufferSize - 1); PtrBuf = (UINT8 *)((UINTN)Buffer + 1); while (PtrBuf <= PtrEnd) { if (*PtrBuf == ']') { break; } PtrBuf ++; } if (PtrBuf > PtrEnd) { // // Not found. Invalid line // return EFI_NOT_FOUND; } if (PtrBuf <= Buffer + 1) { // Empty name return EFI_NOT_FOUND; } // // excluding the heading '[' and tailing ']' // Length = PtrBuf - Buffer - 1; ProfileTrim ( Buffer + 1, &Length ); // // Invalid line if the section name is null // if (Length == 0) { return EFI_NOT_FOUND; } if (!IsValidName((CHAR8 *)Buffer + 1, Length)) { return EFI_INVALID_PARAMETER; } SectionItem = AllocatePool (sizeof (SECTION_ITEM)); if (SectionItem == NULL) { return EFI_OUT_OF_RESOURCES; } SectionItem->PtrSection = NULL; SectionItem->SecNameLen = Length; SectionItem->PtrEntry = NULL; SectionItem->PtrValue = NULL; SectionItem->PtrNext = *SectionHead; *SectionHead = SectionItem; // // Add a trailing '\0' // SectionItem->PtrSection = AllocatePool (Length + 1); if (SectionItem->PtrSection == NULL) { return EFI_OUT_OF_RESOURCES; } // // excluding the heading '[' // CopyMem (SectionItem->PtrSection, Buffer + 1, Length); *(SectionItem->PtrSection + Length) = '\0'; return EFI_SUCCESS; }
/** Add new section item into Section head. @param Buffer Section item data buffer. @param BufferSize Size of section item. @param SectionHead Section item head entry. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated. @retval EFI_SUCCESS Section item is NULL or Section item is added. **/ EFI_STATUS ProfileGetSection ( IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT SECTION_ITEM **SectionHead ) { EFI_STATUS Status; SECTION_ITEM *SectionItem; UINTN Length; UINT8 *PtrBuf; Status = EFI_SUCCESS; // // The first character of Buffer is '[', now we want for ']' // PtrBuf = (UINT8 *)((UINTN)Buffer + BufferSize - 1); while (PtrBuf > Buffer) { if (*PtrBuf == ']') { break; } PtrBuf --; } if (PtrBuf <= Buffer) { // // Not found. Omit this line // return Status; } // // excluding the heading '[' and tailing ']' // Length = PtrBuf - Buffer - 1; ProfileTrim ( Buffer + 1, &Length ); // // omit this line if the section name is null // if (Length == 0) { return Status; } SectionItem = AllocatePool (sizeof (SECTION_ITEM)); if (SectionItem == NULL) { return EFI_OUT_OF_RESOURCES; } SectionItem->ptrSection = NULL; SectionItem->SecNameLen = Length; SectionItem->ptrEntry = NULL; SectionItem->ptrValue = NULL; SectionItem->ptrNext = *SectionHead; *SectionHead = SectionItem; // // Add a trailing '\0' // SectionItem->ptrSection = AllocatePool (Length + 1); if (SectionItem->ptrSection == NULL) { return EFI_OUT_OF_RESOURCES; } // // excluding the heading '[' // CopyMem (SectionItem->ptrSection, Buffer + 1, Length); *(SectionItem->ptrSection + Length) = '\0'; return EFI_SUCCESS; }