summaryrefslogtreecommitdiff
path: root/driver_ais.c
diff options
context:
space:
mode:
authorStefan Roels <sroels-gpsd-dev@42solutions.nl>2015-10-02 22:11:12 -0400
committerJon Schlueter <jschlueter@redhat.com>2015-10-03 08:03:45 -0400
commitfd0055547841966d2b7815cc8ca515879a0ac1ae (patch)
tree2f4a6d79a82ef9e5656288eb24c32c9adf083b2c /driver_ais.c
parent5b926e8abda5371eb2ea9e8b4aca5470e5358f73 (diff)
downloadgpsd-fd0055547841966d2b7815cc8ca515879a0ac1ae.tar.gz
Fixup driver_ais from_sixbit parsing
One of these problems has to do with the removal of spaces at the end of the six bits ascii strings. According to the documentation, the following steps should be taken: - Remove everything from the first @ sign (including the @ sign itself) - Strip trailing spaces from the result The existing code however fails to do so properly in certain circumstances. Consider the following scenarios (all assuming count = 20, e.g. type 5 field shipname): 1) The input string: "ALPHA @@@@@@@@@@@@@@" (single space before first @ character) The from_sixbit function will take i from 0 up to and including 6, where it will see the @ and break. to[6] will then be set to \0. Now the second part of the algorithm kicks in and starts at i=18. I could not find where the field was initialised, so I am not sure whether the field would be zero-filled, @ or space filled or random junk, but in any case either we find spaces or @ signs until we reach i=6, or we break at i>6. This would leave us with: "ALPHA \0..." The space at to[5] will not be cleared. 2) The input string: "ALPHA BRAVO CHARLIE " (single space at the end) The from_sixbit function will take i from 0 up to and including 20, the break condition of the for loop will be met and we set to[20] to \0. At that moment we get to the second part of the algorithm which would look at to[18] which is 'E' and break without removing the trailing space. 3) The input string: "ALPHA BRAVO CHARLI E" (space as character before the last one) The from_sixbit function will take i from 0 up to and including 20, the break condition of the for loop will be met and we set to[20] to \0. At that moment we get to the second part of the algorithm which would look at to[18] which is ' ' and will replace it with a \0, dropping the E at the end. Another spacing problem lies within the type 21 name/name extension field. If the name field would be trimmed, but there is data in the name extension field, the name extension would not be taken into account. Take for instance the following examples: name:"ALPHA BRAVO CHARLI E" name extension:"CHO" (assuming the incorrect space removal) result: "ALPHA BRAVO CHARLI" instead of "ALPHA BRAVO CHARLI ECHO" name:"ALPHA BRAVO CHARLIE " name extension:"DELTA" (assuming removal of the trailing space of name) result "ALPHA BRAVO CHARLIE" instead of "ALPHA BRAVO CHARLIE DELTA" I have created a patch to solve the problems mentioned above. You can find the patch and some examples of real world messages going wrong below. Best regards, Stefan Roels Added sample data from email and applied provided patch Signed-off-by: Jon Schlueter <jschlueter@redhat.com>
Diffstat (limited to 'driver_ais.c')
-rw-r--r--driver_ais.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/driver_ais.c b/driver_ais.c
index 953d3525..1942ef77 100644
--- a/driver_ais.c
+++ b/driver_ais.c
@@ -26,7 +26,7 @@
* Parse the data from the device
*/
-static void from_sixbit(unsigned char *bitvec, uint start, int count, char *to)
+static void from_sixbit_untrimmed(unsigned char *bitvec, uint start, int count, char *to)
/* beginning at bitvec bit start, unpack count sixbit characters */
{
const char sixchr[64] =
@@ -43,12 +43,31 @@ static void from_sixbit(unsigned char *bitvec, uint start, int count, char *to)
to[i] = newchar;
}
to[i] = '\0';
- /* trim spaces on right end */
- for (i = count - 2; i >= 0; i--)
+}
+
+static void trim_spaces_on_right_end(char* to)
+/* trim spaces on right end */
+{
+ int i;
+ for (i = strlen(to) - 1; i >= 0; i--)
+ {
if (to[i] == ' ' || to[i] == '@')
+ {
to[i] = '\0';
+ }
else
+ {
break;
+ }
+ }
+}
+
+static void from_sixbit(unsigned char *bitvec, uint start, int count, char *to)
+/* beginning at bitvec bit start, unpack count sixbit characters and remove trailing
+ * spaces */
+{
+ from_sixbit_untrimmed(bitvec, start, count, to);
+ trim_spaces_on_right_end(to);
}
bool ais_binary_decode(const struct gpsd_errout_t *errout,
@@ -870,7 +889,7 @@ bool ais_binary_decode(const struct gpsd_errout_t *errout,
case 21: /* Aid-to-Navigation Report */
RANGE_CHECK(272, 360);
ais->type21.aid_type = UBITS(38, 5);
- from_sixbit((unsigned char *)bits,
+ from_sixbit_untrimmed((unsigned char *)bits,
43, 20, ais->type21.name);
ais->type21.accuracy = UBITS(163, 1);
ais->type21.lon = SBITS(164, 28);
@@ -889,6 +908,7 @@ bool ais_binary_decode(const struct gpsd_errout_t *errout,
//ais->type21.spare = UBITS(271, 1);
if (strlen(ais->type21.name) == 20 && bitlen > 272)
ENDCHARS(272, ais->type21.name+20);
+ trim_spaces_on_right_end(ais->type21.name);
break;
case 22: /* Channel Management */
PERMISSIVE_LENGTH_CHECK(168)