Problem Statement:
Given a byte array in network byte order, write an algorithm to find the starting bit position of the first occurrence of the 32-bit, big-endian pattern (0xFE6B2840) in the byte array.
The pattern may or may not be byte aligned in the input.
The function htonl() and ntohl() are provided in the C library to convert the endian order from host to network order, and from network to host order respectively:
uint32_t htonl(uint32_t hostlong);
uint32_t ntohl(uint32_t netlong);
Note: network byte order is big-endian, host byte order is little-endian.
Signature of function to implement:
int findPattern(const uint32_t numBytes, const uint8_t data[])
Description of function parameters:
Input parameters:
uint32_t numBytes - The number of bytes in the array named 'data'
uint8_t data[] - The byte stream of data to search
Return:
-1: if the given pattern (0xFE6B2840) is not found.
-2: if the inputData is Null or the size of the 'data' is insufficient to find the pattern (0xFE6B2840).
Otherwise, the pattern is found. Return the starting bit position of the pattern (0xFE6B2840).
Example 1 - Byte Aligned:
Inputs:
numBytes: 8
data: { 0x00, 0x01, 0xFE, 0x6B, 0x28, 0x40, 0x02, 0x03 }
Starting position is here at bit 16
Returns: 16
Example 2 - Non-byte Aligned:
Image 2:
This is the same as example 1, left-shifted by 1 bit!
Inputs:
numBytes: 8
data: { 0x00, 0x03, 0xFC, 0xD6, 0x50, 0x80, 0x04, 0x06 }
Starting position is here at bit 15
(least significant bit of the second byte of the input)
Returns: 15