diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2010-04-14 01:08:51 -0400 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2010-04-14 01:08:51 -0400 |
commit | 12035915af9998df596f1cb3f9ee2406cc99f244 (patch) | |
tree | 460e5a9b421ae45da9e26ba5635fb5916019b9d0 | |
parent | 2daef1ab8aafd0afe69cf80866a0dd390a269370 (diff) | |
download | gpsd-12035915af9998df596f1cb3f9ee2406cc99f244.tar.gz |
Reindent some utility modules. All regression tests pass.
-rw-r--r-- | bsd-base64.c | 338 | ||||
-rw-r--r-- | crc24q.c | 151 | ||||
-rw-r--r-- | hex.c | 212 | ||||
-rw-r--r-- | strl.c | 79 |
4 files changed, 437 insertions, 343 deletions
diff --git a/bsd-base64.c b/bsd-base64.c index 90fbe64f..3ff88abe 100644 --- a/bsd-base64.c +++ b/bsd-base64.c @@ -56,7 +56,7 @@ #define Assert(Cond) if (!(Cond)) abort() static const char Base64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) @@ -124,65 +124,67 @@ static const char Pad64 = '='; /*@ +matchanyintegral -type @*/ int -b64_ntop(unsigned char const *src, size_t srclength, char *target, size_t targsize) +b64_ntop(unsigned char const *src, size_t srclength, char *target, + size_t targsize) { - size_t datalength = 0; - unsigned char input[3]; - unsigned char output[4]; - size_t i; - - while (2 < srclength) { - input[0] = *src++; - input[1] = *src++; - input[2] = *src++; - srclength -= 3; - - output[0] = input[0] >> 2; - output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); - output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); - output[3] = input[2] & 0x3f; - Assert(output[0] < 64); - Assert(output[1] < 64); - Assert(output[2] < 64); - Assert(output[3] < 64); - - if (datalength + 4 > targsize) - return (-1); - target[datalength++] = Base64[output[0]]; - target[datalength++] = Base64[output[1]]; - target[datalength++] = Base64[output[2]]; - target[datalength++] = Base64[output[3]]; - } - - /* Now we worry about padding. */ - if (0 != srclength) { - /* Get what's left. */ - input[0] = input[1] = input[2] = '\0'; - for (i = 0; i < srclength; i++) - input[i] = *src++; - - output[0] = input[0] >> 2; - output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); - output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); - Assert(output[0] < 64); - Assert(output[1] < 64); - Assert(output[2] < 64); - - if (datalength + 4 > targsize) - return (-1); - target[datalength++] = Base64[output[0]]; - target[datalength++] = Base64[output[1]]; - if (srclength == 1) - target[datalength++] = Pad64; - else - target[datalength++] = Base64[output[2]]; - target[datalength++] = Pad64; - } - if (datalength >= targsize) - return (-1); - target[datalength] = '\0'; /* Returned value doesn't count \0. */ - return (datalength); + size_t datalength = 0; + unsigned char input[3]; + unsigned char output[4]; + size_t i; + + while (2 < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0 != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /* Returned value doesn't count \0. */ + return (datalength); } + /*@ -matchanyintegral +type @*/ /* skips all whitespace anywhere. @@ -192,122 +194,122 @@ b64_ntop(unsigned char const *src, size_t srclength, char *target, size_t targsi */ /*@ +matchanyintegral +charint @*/ -int -b64_pton(char const *src, unsigned char *target, size_t targsize) +int b64_pton(char const *src, unsigned char *target, size_t targsize) { - size_t tarindex; - int state, ch; - char *pos; - - state = 0; - tarindex = 0; - - while ((ch = *src++) != '\0') { - if (isspace(ch)) /* Skip whitespace anywhere. */ - continue; - - if (ch == Pad64) - break; - - if ((pos = strchr(Base64, ch)) == NULL) /* A non-base64 character. */ - return (-1); - - switch (state) { - case 0: - if (target) { - if (tarindex >= targsize) - return (-1); - target[tarindex] = (pos - Base64) << 2; - } - state = 1; - break; - case 1: - if (target) { - if (tarindex + 1 >= targsize) - return (-1); - target[tarindex] |= (pos - Base64) >> 4; - target[tarindex+1] = ((pos - Base64) & 0x0f) - << 4 ; - } - tarindex++; - state = 2; - break; - case 2: - if (target) { - if (tarindex + 1 >= targsize) - return (-1); - target[tarindex] |= (pos - Base64) >> 2; - target[tarindex+1] = ((pos - Base64) & 0x03) - << 6; - } - tarindex++; - state = 3; - break; - case 3: - if (target) { - if (tarindex >= targsize) - return (-1); - target[tarindex] |= (pos - Base64); - } - tarindex++; - state = 0; - break; - } + size_t tarindex; + int state, ch; + char *pos; + + state = 0; + tarindex = 0; + + while ((ch = *src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + if ((pos = strchr(Base64, ch)) == NULL) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 4; + target[tarindex + 1] = ((pos - Base64) & 0x0f) + << 4; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 2; + target[tarindex + 1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; } - + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /* We got a pad char. */ + ch = *src++; /* Skip it, get next. */ + switch (state) { + case 0: /* Invalid = in first position */ + case 1: /* Invalid = in second position */ + return (-1); + + case 2: /* Valid, means one byte of info */ + /* Skip any number of spaces. */ + for (; ch != '\0'; ch = *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /* Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + /*@ -casebreak @*/ + case 3: /* Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for (; ch != '\0'; ch = *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target != 0 && target[tarindex] != 0) + return (-1); + } + } else { /* - * We are done decoding Base-64 chars. Let's see if we ended - * on a byte boundary, and/or with erroneous trailing characters. + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. */ + if (state != 0) + return (-1); + } - if (ch == Pad64) { /* We got a pad char. */ - ch = *src++; /* Skip it, get next. */ - switch (state) { - case 0: /* Invalid = in first position */ - case 1: /* Invalid = in second position */ - return (-1); - - case 2: /* Valid, means one byte of info */ - /* Skip any number of spaces. */ - for (; ch != '\0'; ch = *src++) - if (!isspace(ch)) - break; - /* Make sure there is another trailing = sign. */ - if (ch != Pad64) - return (-1); - ch = *src++; /* Skip the = */ - /* Fall through to "single trailing =" case. */ - /* FALLTHROUGH */ - /*@ -casebreak @*/ - case 3: /* Valid, means two bytes of info */ - /* - * We know this char is an =. Is there anything but - * whitespace after it? - */ - for (; ch != '\0'; ch = *src++) - if (!isspace(ch)) - return (-1); - - /* - * Now make sure for cases 2 and 3 that the "extra" - * bits that slopped past the last full byte were - * zeros. If we don't check them, they become a - * subliminal channel. - */ - if (target!=0 && target[tarindex] != 0) - return (-1); - } - } else { - /* - * We ended by seeing the end of the string. Make sure we - * have no partial bytes lying around. - */ - if (state != 0) - return (-1); - } - - return (tarindex); + return (tarindex); } + /*@ +matchanyintegral -charint @*/ #endif /* !defined(HAVE_B64_NTOP) && !defined(HAVE___B64_NTOP) */ @@ -40,10 +40,9 @@ unsigned table[25]; #define CRCSEED 0 /* could be NZ to detect leading zeros */ -#define CRCPOLY 0x1864CFB /* encodes all info about the polynomial */ +#define CRCPOLY 0x1864CFB /* encodes all info about the polynomial */ -static void -crc_init(unsigned table[256]) +static void crc_init(unsigned table[256]) { unsigned i, j; unsigned h; @@ -55,7 +54,7 @@ crc_init(unsigned table[256]) if ((h <<= 1) & 0x1000000) h ^= CRCPOLY; for (j = 0; j < i; j++) - table[i+j] = table[j] ^ h; + table[i + j] = table[j] ^ h; } } @@ -76,70 +75,70 @@ int main(int argc, char *argv[]) #endif static const unsigned crc24q[256] = { - 0x00000000, 0x01864CFB, 0x028AD50D, 0x030C99F6, - 0x0493E6E1, 0x0515AA1A, 0x061933EC, 0x079F7F17, - 0x08A18139, 0x0927CDC2, 0x0A2B5434, 0x0BAD18CF, - 0x0C3267D8, 0x0DB42B23, 0x0EB8B2D5, 0x0F3EFE2E, - 0x10C54E89, 0x11430272, 0x124F9B84, 0x13C9D77F, - 0x1456A868, 0x15D0E493, 0x16DC7D65, 0x175A319E, - 0x1864CFB0, 0x19E2834B, 0x1AEE1ABD, 0x1B685646, - 0x1CF72951, 0x1D7165AA, 0x1E7DFC5C, 0x1FFBB0A7, - 0x200CD1E9, 0x218A9D12, 0x228604E4, 0x2300481F, - 0x249F3708, 0x25197BF3, 0x2615E205, 0x2793AEFE, - 0x28AD50D0, 0x292B1C2B, 0x2A2785DD, 0x2BA1C926, - 0x2C3EB631, 0x2DB8FACA, 0x2EB4633C, 0x2F322FC7, - 0x30C99F60, 0x314FD39B, 0x32434A6D, 0x33C50696, - 0x345A7981, 0x35DC357A, 0x36D0AC8C, 0x3756E077, - 0x38681E59, 0x39EE52A2, 0x3AE2CB54, 0x3B6487AF, - 0x3CFBF8B8, 0x3D7DB443, 0x3E712DB5, 0x3FF7614E, - 0x4019A3D2, 0x419FEF29, 0x429376DF, 0x43153A24, - 0x448A4533, 0x450C09C8, 0x4600903E, 0x4786DCC5, - 0x48B822EB, 0x493E6E10, 0x4A32F7E6, 0x4BB4BB1D, - 0x4C2BC40A, 0x4DAD88F1, 0x4EA11107, 0x4F275DFC, - 0x50DCED5B, 0x515AA1A0, 0x52563856, 0x53D074AD, - 0x544F0BBA, 0x55C94741, 0x56C5DEB7, 0x5743924C, - 0x587D6C62, 0x59FB2099, 0x5AF7B96F, 0x5B71F594, - 0x5CEE8A83, 0x5D68C678, 0x5E645F8E, 0x5FE21375, - 0x6015723B, 0x61933EC0, 0x629FA736, 0x6319EBCD, - 0x648694DA, 0x6500D821, 0x660C41D7, 0x678A0D2C, - 0x68B4F302, 0x6932BFF9, 0x6A3E260F, 0x6BB86AF4, - 0x6C2715E3, 0x6DA15918, 0x6EADC0EE, 0x6F2B8C15, - 0x70D03CB2, 0x71567049, 0x725AE9BF, 0x73DCA544, - 0x7443DA53, 0x75C596A8, 0x76C90F5E, 0x774F43A5, - 0x7871BD8B, 0x79F7F170, 0x7AFB6886, 0x7B7D247D, - 0x7CE25B6A, 0x7D641791, 0x7E688E67, 0x7FEEC29C, - 0x803347A4, 0x81B50B5F, 0x82B992A9, 0x833FDE52, - 0x84A0A145, 0x8526EDBE, 0x862A7448, 0x87AC38B3, - 0x8892C69D, 0x89148A66, 0x8A181390, 0x8B9E5F6B, - 0x8C01207C, 0x8D876C87, 0x8E8BF571, 0x8F0DB98A, - 0x90F6092D, 0x917045D6, 0x927CDC20, 0x93FA90DB, - 0x9465EFCC, 0x95E3A337, 0x96EF3AC1, 0x9769763A, - 0x98578814, 0x99D1C4EF, 0x9ADD5D19, 0x9B5B11E2, - 0x9CC46EF5, 0x9D42220E, 0x9E4EBBF8, 0x9FC8F703, - 0xA03F964D, 0xA1B9DAB6, 0xA2B54340, 0xA3330FBB, - 0xA4AC70AC, 0xA52A3C57, 0xA626A5A1, 0xA7A0E95A, - 0xA89E1774, 0xA9185B8F, 0xAA14C279, 0xAB928E82, - 0xAC0DF195, 0xAD8BBD6E, 0xAE872498, 0xAF016863, - 0xB0FAD8C4, 0xB17C943F, 0xB2700DC9, 0xB3F64132, - 0xB4693E25, 0xB5EF72DE, 0xB6E3EB28, 0xB765A7D3, - 0xB85B59FD, 0xB9DD1506, 0xBAD18CF0, 0xBB57C00B, - 0xBCC8BF1C, 0xBD4EF3E7, 0xBE426A11, 0xBFC426EA, - 0xC02AE476, 0xC1ACA88D, 0xC2A0317B, 0xC3267D80, - 0xC4B90297, 0xC53F4E6C, 0xC633D79A, 0xC7B59B61, - 0xC88B654F, 0xC90D29B4, 0xCA01B042, 0xCB87FCB9, - 0xCC1883AE, 0xCD9ECF55, 0xCE9256A3, 0xCF141A58, - 0xD0EFAAFF, 0xD169E604, 0xD2657FF2, 0xD3E33309, - 0xD47C4C1E, 0xD5FA00E5, 0xD6F69913, 0xD770D5E8, - 0xD84E2BC6, 0xD9C8673D, 0xDAC4FECB, 0xDB42B230, - 0xDCDDCD27, 0xDD5B81DC, 0xDE57182A, 0xDFD154D1, - 0xE026359F, 0xE1A07964, 0xE2ACE092, 0xE32AAC69, - 0xE4B5D37E, 0xE5339F85, 0xE63F0673, 0xE7B94A88, - 0xE887B4A6, 0xE901F85D, 0xEA0D61AB, 0xEB8B2D50, - 0xEC145247, 0xED921EBC, 0xEE9E874A, 0xEF18CBB1, - 0xF0E37B16, 0xF16537ED, 0xF269AE1B, 0xF3EFE2E0, - 0xF4709DF7, 0xF5F6D10C, 0xF6FA48FA, 0xF77C0401, - 0xF842FA2F, 0xF9C4B6D4, 0xFAC82F22, 0xFB4E63D9, - 0xFCD11CCE, 0xFD575035, 0xFE5BC9C3, 0xFFDD8538, + 0x00000000, 0x01864CFB, 0x028AD50D, 0x030C99F6, + 0x0493E6E1, 0x0515AA1A, 0x061933EC, 0x079F7F17, + 0x08A18139, 0x0927CDC2, 0x0A2B5434, 0x0BAD18CF, + 0x0C3267D8, 0x0DB42B23, 0x0EB8B2D5, 0x0F3EFE2E, + 0x10C54E89, 0x11430272, 0x124F9B84, 0x13C9D77F, + 0x1456A868, 0x15D0E493, 0x16DC7D65, 0x175A319E, + 0x1864CFB0, 0x19E2834B, 0x1AEE1ABD, 0x1B685646, + 0x1CF72951, 0x1D7165AA, 0x1E7DFC5C, 0x1FFBB0A7, + 0x200CD1E9, 0x218A9D12, 0x228604E4, 0x2300481F, + 0x249F3708, 0x25197BF3, 0x2615E205, 0x2793AEFE, + 0x28AD50D0, 0x292B1C2B, 0x2A2785DD, 0x2BA1C926, + 0x2C3EB631, 0x2DB8FACA, 0x2EB4633C, 0x2F322FC7, + 0x30C99F60, 0x314FD39B, 0x32434A6D, 0x33C50696, + 0x345A7981, 0x35DC357A, 0x36D0AC8C, 0x3756E077, + 0x38681E59, 0x39EE52A2, 0x3AE2CB54, 0x3B6487AF, + 0x3CFBF8B8, 0x3D7DB443, 0x3E712DB5, 0x3FF7614E, + 0x4019A3D2, 0x419FEF29, 0x429376DF, 0x43153A24, + 0x448A4533, 0x450C09C8, 0x4600903E, 0x4786DCC5, + 0x48B822EB, 0x493E6E10, 0x4A32F7E6, 0x4BB4BB1D, + 0x4C2BC40A, 0x4DAD88F1, 0x4EA11107, 0x4F275DFC, + 0x50DCED5B, 0x515AA1A0, 0x52563856, 0x53D074AD, + 0x544F0BBA, 0x55C94741, 0x56C5DEB7, 0x5743924C, + 0x587D6C62, 0x59FB2099, 0x5AF7B96F, 0x5B71F594, + 0x5CEE8A83, 0x5D68C678, 0x5E645F8E, 0x5FE21375, + 0x6015723B, 0x61933EC0, 0x629FA736, 0x6319EBCD, + 0x648694DA, 0x6500D821, 0x660C41D7, 0x678A0D2C, + 0x68B4F302, 0x6932BFF9, 0x6A3E260F, 0x6BB86AF4, + 0x6C2715E3, 0x6DA15918, 0x6EADC0EE, 0x6F2B8C15, + 0x70D03CB2, 0x71567049, 0x725AE9BF, 0x73DCA544, + 0x7443DA53, 0x75C596A8, 0x76C90F5E, 0x774F43A5, + 0x7871BD8B, 0x79F7F170, 0x7AFB6886, 0x7B7D247D, + 0x7CE25B6A, 0x7D641791, 0x7E688E67, 0x7FEEC29C, + 0x803347A4, 0x81B50B5F, 0x82B992A9, 0x833FDE52, + 0x84A0A145, 0x8526EDBE, 0x862A7448, 0x87AC38B3, + 0x8892C69D, 0x89148A66, 0x8A181390, 0x8B9E5F6B, + 0x8C01207C, 0x8D876C87, 0x8E8BF571, 0x8F0DB98A, + 0x90F6092D, 0x917045D6, 0x927CDC20, 0x93FA90DB, + 0x9465EFCC, 0x95E3A337, 0x96EF3AC1, 0x9769763A, + 0x98578814, 0x99D1C4EF, 0x9ADD5D19, 0x9B5B11E2, + 0x9CC46EF5, 0x9D42220E, 0x9E4EBBF8, 0x9FC8F703, + 0xA03F964D, 0xA1B9DAB6, 0xA2B54340, 0xA3330FBB, + 0xA4AC70AC, 0xA52A3C57, 0xA626A5A1, 0xA7A0E95A, + 0xA89E1774, 0xA9185B8F, 0xAA14C279, 0xAB928E82, + 0xAC0DF195, 0xAD8BBD6E, 0xAE872498, 0xAF016863, + 0xB0FAD8C4, 0xB17C943F, 0xB2700DC9, 0xB3F64132, + 0xB4693E25, 0xB5EF72DE, 0xB6E3EB28, 0xB765A7D3, + 0xB85B59FD, 0xB9DD1506, 0xBAD18CF0, 0xBB57C00B, + 0xBCC8BF1C, 0xBD4EF3E7, 0xBE426A11, 0xBFC426EA, + 0xC02AE476, 0xC1ACA88D, 0xC2A0317B, 0xC3267D80, + 0xC4B90297, 0xC53F4E6C, 0xC633D79A, 0xC7B59B61, + 0xC88B654F, 0xC90D29B4, 0xCA01B042, 0xCB87FCB9, + 0xCC1883AE, 0xCD9ECF55, 0xCE9256A3, 0xCF141A58, + 0xD0EFAAFF, 0xD169E604, 0xD2657FF2, 0xD3E33309, + 0xD47C4C1E, 0xD5FA00E5, 0xD6F69913, 0xD770D5E8, + 0xD84E2BC6, 0xD9C8673D, 0xDAC4FECB, 0xDB42B230, + 0xDCDDCD27, 0xDD5B81DC, 0xDE57182A, 0xDFD154D1, + 0xE026359F, 0xE1A07964, 0xE2ACE092, 0xE32AAC69, + 0xE4B5D37E, 0xE5339F85, 0xE63F0673, 0xE7B94A88, + 0xE887B4A6, 0xE901F85D, 0xEA0D61AB, 0xEB8B2D50, + 0xEC145247, 0xED921EBC, 0xEE9E874A, 0xEF18CBB1, + 0xF0E37B16, 0xF16537ED, 0xF269AE1B, 0xF3EFE2E0, + 0xF4709DF7, 0xF5F6D10C, 0xF6FA48FA, 0xF77C0401, + 0xF842FA2F, 0xF9C4B6D4, 0xFAC82F22, 0xFB4E63D9, + 0xFCD11CCE, 0xFD575035, 0xFE5BC9C3, 0xFFDD8538, }; unsigned crc24q_hash(unsigned char *data, int len) @@ -148,7 +147,7 @@ unsigned crc24q_hash(unsigned char *data, int len) unsigned crc = 0; for (i = 0; i < len; i++) { - crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc>>16)]; + crc = (crc << 8) ^ crc24q[data[i] ^ (unsigned char)(crc >> 16)]; } crc = (crc & 0x00ffffff); @@ -164,17 +163,15 @@ void crc24q_sign(unsigned char *data, int len) { unsigned crc = crc24q_hash(data, len); - data[len] = HI(crc); - data[len+1] = MID(crc); - data[len+2] = LO(crc); + data[len] = HI(crc); + data[len + 1] = MID(crc); + data[len + 2] = LO(crc); } bool crc24q_check(unsigned char *data, int len) { - unsigned crc = crc24q_hash(data, len-3); + unsigned crc = crc24q_hash(data, len - 3); - return (((data[len-3] == HI(crc)) && - (data[len-2] == MID(crc)) && - (data[len-1] == LO(crc)))); + return (((data[len - 3] == HI(crc)) && + (data[len - 2] == MID(crc)) && (data[len - 1] == LO(crc)))); } - @@ -22,7 +22,7 @@ int gpsd_hexdump_level = -1; * speed, chatty protocols, this can save a lot of CPU. */ char *gpsd_hexdump_wrapper(const void *binbuf, size_t binbuflen, - int msg_debug_level) + int msg_debug_level) { #ifndef SQUELCH_ENABLE if (msg_debug_level <= gpsd_hexdump_level) @@ -33,44 +33,47 @@ char *gpsd_hexdump_wrapper(const void *binbuf, size_t binbuflen, char /*@ observer @*/ *gpsd_hexdump(const void *binbuf, size_t binbuflen) { - static char hexbuf[MAX_PACKET_LENGTH*2+1]; + static char hexbuf[MAX_PACKET_LENGTH * 2 + 1]; #ifndef SQUELCH_ENABLE size_t i, j = 0; - size_t len = (size_t)((binbuflen > MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen); + size_t len = + (size_t) ((binbuflen > + MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen); const char *ibuf = (const char *)binbuf; const char *hexchar = "0123456789abcdef"; - if (NULL == binbuf || 0 == binbuflen) + if (NULL == binbuf || 0 == binbuflen) return ""; /*@ -shiftimplementation @*/ for (i = 0; i < len; i++) { - hexbuf[j++] = hexchar[ (ibuf[i]&0xf0)>>4 ]; - hexbuf[j++] = hexchar[ ibuf[i]&0x0f ]; + hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4]; + hexbuf[j++] = hexchar[ibuf[i] & 0x0f]; } /*@ +shiftimplementation @*/ - hexbuf[j] ='\0'; + hexbuf[j] = '\0'; #else /* SQUELCH defined */ hexbuf[0] = '\0'; #endif /* SQUELCH_ENABLE */ return hexbuf; } -int gpsd_hexpack(/*@in@*/const char *src, /*@out@*/char *dst, size_t len) { -/* hex2bin source string to destination - destination can be same as source */ +int gpsd_hexpack( /*@in@*/ const char *src, /*@out@ */ char *dst, size_t len) +{ +/* hex2bin source string to destination - destination can be same as source */ int i, k, l; /*@ -mustdefine @*/ l = (int)(strlen(src) / 2); - if ((l < 1) || ((size_t)l > len)) + if ((l < 1) || ((size_t) l > len)) return -2; for (i = 0; i < l; i++) - if ((k = hex2bin(src+i*2)) != -1) + if ((k = hex2bin(src + i * 2)) != -1) dst[i] = (char)(k & 0xff); else return -1; - (void)memset(dst+i, '\0', (size_t)(len-i)); + (void)memset(dst + i, '\0', (size_t) (len - i)); return l; /*@ +mustdefine @*/ } @@ -101,11 +104,12 @@ int hex2bin(const char *s) else return -1; - return ((a<<4) + b); + return ((a << 4) + b); } + /*@ -charint +shiftimplementation @*/ -ssize_t hex_escapes(/*@out@*/char *cooked, const char *raw) +ssize_t hex_escapes( /*@out@*/ char *cooked, const char *raw) /* interpret C-style hex escapes */ { char c, *cookend; @@ -115,62 +119,154 @@ ssize_t hex_escapes(/*@out@*/char *cooked, const char *raw) if (*raw != '\\') *cookend++ = *raw; else { - switch(*++raw) { - case 'b': *cookend++ = '\b'; break; - case 'e': *cookend++ = '\x1b'; break; - case 'f': *cookend++ = '\f'; break; - case 'n': *cookend++ = '\n'; break; - case 'r': *cookend++ = '\r'; break; - case 't': *cookend++ = '\r'; break; - case 'v': *cookend++ = '\v'; break; + switch (*++raw) { + case 'b': + *cookend++ = '\b'; + break; + case 'e': + *cookend++ = '\x1b'; + break; + case 'f': + *cookend++ = '\f'; + break; + case 'n': + *cookend++ = '\n'; + break; + case 'r': + *cookend++ = '\r'; + break; + case 't': + *cookend++ = '\r'; + break; + case 'v': + *cookend++ = '\v'; + break; case 'x': - switch(*++raw) { - case '0': c = (char)0x00; break; - case '1': c = (char)0x10; break; - case '2': c = (char)0x20; break; - case '3': c = (char)0x30; break; - case '4': c = (char)0x40; break; - case '5': c = (char)0x50; break; - case '6': c = (char)0x60; break; - case '7': c = (char)0x70; break; - case '8': c = (char)0x80; break; - case '9': c = (char)0x90; break; - case 'A': case 'a': c = (char)0xa0; break; - case 'B': case 'b': c = (char)0xb0; break; - case 'C': case 'c': c = (char)0xc0; break; - case 'D': case 'd': c = (char)0xd0; break; - case 'E': case 'e': c = (char)0xe0; break; - case 'F': case 'f': c = (char)0xf0; break; + switch (*++raw) { + case '0': + c = (char)0x00; + break; + case '1': + c = (char)0x10; + break; + case '2': + c = (char)0x20; + break; + case '3': + c = (char)0x30; + break; + case '4': + c = (char)0x40; + break; + case '5': + c = (char)0x50; + break; + case '6': + c = (char)0x60; + break; + case '7': + c = (char)0x70; + break; + case '8': + c = (char)0x80; + break; + case '9': + c = (char)0x90; + break; + case 'A': + case 'a': + c = (char)0xa0; + break; + case 'B': + case 'b': + c = (char)0xb0; + break; + case 'C': + case 'c': + c = (char)0xc0; + break; + case 'D': + case 'd': + c = (char)0xd0; + break; + case 'E': + case 'e': + c = (char)0xe0; + break; + case 'F': + case 'f': + c = (char)0xf0; + break; default: return -1; } - switch(*++raw) { - case '0': c += 0x00; break; - case '1': c += 0x01; break; - case '2': c += 0x02; break; - case '3': c += 0x03; break; - case '4': c += 0x04; break; - case '5': c += 0x05; break; - case '6': c += 0x06; break; - case '7': c += 0x07; break; - case '8': c += 0x08; break; - case '9': c += 0x09; break; - case 'A': case 'a': c += 0x0a; break; - case 'B': case 'b': c += 0x0b; break; - case 'C': case 'c': c += 0x0c; break; - case 'D': case 'd': c += 0x0d; break; - case 'E': case 'e': c += 0x0e; break; - case 'F': case 'f': c += 0x0f; break; + switch (*++raw) { + case '0': + c += 0x00; + break; + case '1': + c += 0x01; + break; + case '2': + c += 0x02; + break; + case '3': + c += 0x03; + break; + case '4': + c += 0x04; + break; + case '5': + c += 0x05; + break; + case '6': + c += 0x06; + break; + case '7': + c += 0x07; + break; + case '8': + c += 0x08; + break; + case '9': + c += 0x09; + break; + case 'A': + case 'a': + c += 0x0a; + break; + case 'B': + case 'b': + c += 0x0b; + break; + case 'C': + case 'c': + c += 0x0c; + break; + case 'D': + case 'd': + c += 0x0d; + break; + case 'E': + case 'e': + c += 0x0e; + break; + case 'F': + case 'f': + c += 0x0f; + break; default: return -2; } *cookend++ = c; break; - case '\\': *cookend++ = '\\'; break; + case '\\': + *cookend++ = '\\'; + break; default: return -3; } } - return (ssize_t)(cookend - cooked); + return (ssize_t) (cookend - cooked); /*@ +charint +mustdefine +compdef @*/ } @@ -37,33 +37,33 @@ * If retval >= siz, truncation occurred. */ /*@ -usedef -mustdefine @*/ -size_t -strlcat(char *dst, const char *src, size_t siz) +size_t strlcat(char *dst, const char *src, size_t siz) { - char *d = dst; - const char *s = src; - size_t n = siz; - size_t dlen; + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = (size_t)(d - dst); - n = siz - dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = (size_t) (d - dst); + n = siz - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; + if (n == 0) + return (dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; } - *d = '\0'; + s++; + } + *d = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ + return (dlen + (s - src)); /* count does not include NUL */ } + /*@ +usedef +mustdefine @*/ #endif /* HAVE_STRLCAT */ @@ -91,29 +91,28 @@ strlcat(char *dst, const char *src, size_t siz) * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t -strlcpy(char *dst, const char *src, size_t siz) +size_t strlcpy(char *dst, const char *src, size_t siz) { - char *d = dst; - const char *s = src; - size_t n = siz; + char *d = dst; + const char *s = src; + size_t n = siz; - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; } + } - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++ != '\0') - continue; - } + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++ != '\0') + continue; + } - return((size_t)(s - src - 1)); /* count does not include NUL */ + return ((size_t) (s - src - 1)); /* count does not include NUL */ } #endif /* HAVE_STRLCPY */ |