core/wire/wire_read.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file wire_read.h | ||
| 3 | * @brief Reads the values the phone packs into a message. | ||
| 4 | * | ||
| 5 | * The phone is free to send an integer one, two, or four bytes wide, and it says which by the | ||
| 6 | * length it puts beside the value rather than by the type. The values sit back to back with | ||
| 7 | * nothing between them, so a reader that takes the type's word for the width and grabs four bytes | ||
| 8 | * off a one-byte value walks straight into whatever is packed behind it. That is what these are | ||
| 9 | * for: the width decides how many bytes get read, and anything else is refused. A number sent as | ||
| 10 | * text is read here too, so a setting takes it whichever way the phone sent it. | ||
| 11 | * | ||
| 12 | * Header only on purpose. There is one caller on the watch (tuple_read.c), so each body folds into | ||
| 13 | * it and nothing is spent on getting there. | ||
| 14 | * | ||
| 15 | * @ingroup lib_core | ||
| 16 | */ | ||
| 17 | #pragma once | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include <stdint.h> | ||
| 20 | |||
| 21 | #include "io/bytes_le.h" | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @addtogroup lib_core | ||
| 25 | * @{ | ||
| 26 | */ | ||
| 27 | |||
| 28 | /** | ||
| 29 | * @brief Reads a little-endian integer of exactly @p length bytes. | ||
| 30 | * | ||
| 31 | * @param is_signed Whether the value is signed. Decides only what the top bit means. | ||
| 32 | * @param length How many bytes the value occupies. 1, 2, and 4 are the widths a sender may use, | ||
| 33 | * and anything else is refused rather than guessed at. | ||
| 34 | * @param value The first byte of the value. | ||
| 35 | * @param fallback Returned when the width is not one a sender may use, or there is no value. | ||
| 36 | * @return The value, or @p fallback. | ||
| 37 | */ | ||
| 38 | 10 | static inline int32_t wire_int_or(bool is_signed, uint16_t length, const uint8_t *value, int32_t fallback) | |
| 39 | { | ||
| 40 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 9 times.
|
10 | if (!value) |
| 41 | { | ||
| 42 | 1 | return fallback; | |
| 43 | } | ||
| 44 | |||
| 45 |
4/4✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 9 taken 2 times.
✓ Branch 4 → 15 taken 2 times.
✓ Branch 4 → 16 taken 2 times.
|
9 | switch (length) |
| 46 | { | ||
| 47 | // a byte read signed is -128 to 127 and unsigned is 0 to 255 | ||
| 48 | // that is the only thing the signedness decides at this width | ||
| 49 |
2/2✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 1 time.
|
3 | case 1: return is_signed ? (int32_t)(int8_t)value[0] : (int32_t)value[0]; |
| 50 | |||
| 51 | // read the pair as signed and take the sign back off when it is not wanted | ||
| 52 | // so the unsigned half lands on 0 to 65535 rather than going negative at the top | ||
| 53 | 1 | case 2: return is_signed ? (int32_t)read_i16_le(value) | |
| 54 |
2/2✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 12 taken 1 time.
|
3 | : (int32_t)(uint16_t)read_i16_le(value); |
| 55 | |||
| 56 | // four bytes fill the whole width either way so the bits are the same | ||
| 57 | // and only the caller's reading of the top one differs | ||
| 58 | 2 | case 4: return read_i32_le(value); | |
| 59 | |||
| 60 | 2 | default: return fallback; | |
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @brief Whether a string of @p length bytes carries its terminator inside itself. | ||
| 66 | * | ||
| 67 | * A sender is supposed to include the terminator and nothing re-checks that it did. A string | ||
| 68 | * without one is not a string: reading it runs on past the end looking for a zero byte that | ||
| 69 | * belongs to something else, or to nothing at all. | ||
| 70 | * | ||
| 71 | * @param s The first byte of the string. | ||
| 72 | * @param length How many bytes the string occupies, terminator included. | ||
| 73 | * @return Whether it is safe to read as a C string. | ||
| 74 | */ | ||
| 75 | 5 | static inline bool wire_cstring_terminated(const char *s, uint16_t length) | |
| 76 | { | ||
| 77 |
5/6✓ Branch 2 → 3 taken 5 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 6 taken 1 time.
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 2 times.
|
5 | return s && length > 0 && s[length - 1] == '\0'; |
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @brief Reads text holding only digits as the whole number it spells, such as a select's "3". | ||
| 82 | * | ||
| 83 | * Clay sends a select's value as text, so a choice of 3 arrives as "3". One or more digits and | ||
| 84 | * nothing else is a number. A sign, a space at either end, or anything after the digits gives the | ||
| 85 | * fallback, so "3a" is turned away rather than read as 3. | ||
| 86 | * | ||
| 87 | * A run of digits too big for an int32_t is still a number, just a big one, so it reads as | ||
| 88 | * INT32_MAX. That keeps it apart from the fallback, and a choice then takes it the way it takes | ||
| 89 | * "300", as past the end of its list. | ||
| 90 | * | ||
| 91 | * @param text The text, already checked to end inside its own length, or NULL. | ||
| 92 | * @param fallback Returned when there is no text, or it holds anything but digits. | ||
| 93 | * @return The number, INT32_MAX for digits past it, or @p fallback. | ||
| 94 | */ | ||
| 95 | 15 | static inline int32_t wire_digits_or(const char *text, int32_t fallback) | |
| 96 | { | ||
| 97 |
4/4✓ Branch 2 → 3 taken 14 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 13 times.
|
15 | if (!text || text[0] == '\0') |
| 98 | { | ||
| 99 | 2 | return fallback; | |
| 100 | } | ||
| 101 | |||
| 102 | 13 | int32_t value = 0; | |
| 103 |
2/2✓ Branch 13 → 6 taken 74 times.
✓ Branch 13 → 14 taken 6 times.
|
80 | for (const char *cursor = text; *cursor != '\0'; cursor++) |
| 104 | { | ||
| 105 |
4/4✓ Branch 6 → 7 taken 70 times.
✓ Branch 6 → 8 taken 4 times.
✓ Branch 7 → 8 taken 3 times.
✓ Branch 7 → 9 taken 67 times.
|
74 | if (*cursor < '0' || *cursor > '9') |
| 106 | { | ||
| 107 | 7 | return fallback; | |
| 108 | } | ||
| 109 | |||
| 110 | 67 | int32_t digit = *cursor - '0'; | |
| 111 | |||
| 112 | // held at the top before the next digit goes on, since a sum past INT32_MAX is undefined | ||
| 113 | // in C. the walk carries on so a letter further along still turns the text away | ||
| 114 |
2/2✓ Branch 9 → 10 taken 23 times.
✓ Branch 9 → 11 taken 44 times.
|
67 | if (value > (INT32_MAX - digit) / 10) |
| 115 | { | ||
| 116 | 23 | value = INT32_MAX; | |
| 117 | 23 | continue; | |
| 118 | } | ||
| 119 | |||
| 120 | 44 | value = value * 10 + digit; | |
| 121 | } | ||
| 122 | |||
| 123 | 6 | return value; | |
| 124 | } | ||
| 125 | |||
| 126 | /** @} */ | ||
| 127 |