seac_screen_common.c (1731B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2025 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include "seac_screen_common.h" 23 #include "context-private.h" 24 #include "checksum.h" 25 #include "array.h" 26 27 int 28 seac_screen_record_isvalid (dc_context_t *context, const unsigned char data[], unsigned int size, unsigned int type, unsigned int id) 29 { 30 // Check the record size. 31 if (size != SZ_RECORD) { 32 ERROR (context, "Unexpected record size (%u).", size); 33 return 0; 34 } 35 36 // Check the record checksum. 37 unsigned short csum = checksum_crc16_ccitt (data, size, 0xFFFF, 0x0000); 38 if (csum != 0) { 39 ERROR (context, "Unexpected record checksum (%04x).", csum); 40 return 0; 41 } 42 43 // Check the record type. 44 unsigned int rtype = data[size - 3]; 45 if (rtype != type) { 46 ERROR (context, "Unexpected record type (%02x %02x).", rtype, type); 47 return 0; 48 } 49 50 // Check the record id. 51 unsigned int rid = array_uint32_le (data); 52 if (rid != id) { 53 ERROR (context, "Unexpected record id (%u %u).", rid, id); 54 return 0; 55 } 56 57 return 1; 58 }