/* * Copyright (C) 2026 Peter Mikkelsen * * This file is part of aplwc. * * Aplwc is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Aplwc is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with aplwc. If not, see . */ #include #include #include uint8_t bad1[] = {0xff,0}; uint8_t bad2[] = {0x80,0}; uint8_t bad3[] = {0xc0,0x80,0}; uint8_t bad4[] = {0xc0,0x40,0}; uint8_t bad5[] = {0xe0,0x80,0x80,0}; uint8_t bad6[] = {0xc0,0}; uint8_t bad7[] = {0xF0,0x80,0x80,0x80,0}; uint8_t bad8[] = {0xF7,0xBF,0xBF,0xBF,0}; uint8_t good1[] = {0x61,0x70,0x6c,0x77,0x63,0}; uint8_t good2[] = {0x41,0xe2,0x89,0xa2,0xce,0x91,0x2e,0}; uint8_t good3[] = {0xed,0x95,0x9c,0xea,0xb5,0xad,0xec,0x96,0xb4,0}; uint8_t good4[] = {0xe6,0x97,0xa5,0xe6,0x9c,0xac,0xe8,0xaa,0x9e,0}; uint8_t good5[] = {0xef,0xbb,0xbf,0xf0,0xa3,0x8e,0xb4,0}; static void * my_alloc(size_t size, size_t *actual) { if(actual) *actual = size; return malloc(size); } static void my_dealloc(void *p) { free(p); } int main(void) { struct aplwc *aplwc; AplwcLine line; struct aplwc_fns fns; enum aplwc_rc ret; size_t i; uint8_t *bad_tests[] = { bad1, bad2, bad3, bad4, bad5, bad6, bad7, bad8 }; uint8_t *good_tests[] = { good1, good2, good3, good4, good5 }; memset(&fns, 0, sizeof(fns)); fns.alloc = my_alloc; fns.dealloc = my_dealloc; aplwc = aplwc_new(fns, &ret); if(ret) return 1; for(i = 0; i < (sizeof(bad_tests)/sizeof(*bad_tests)); i++){ aplwc_read(aplwc, (char *)bad_tests[i], &line); if(aplwc_valid_line(&line)) return 1; if(aplwc_last_error(aplwc) != APLWC_INVALID_UTF8) return 1; aplwc_free_line(&line); } for(i = 0; i < (sizeof(good_tests)/sizeof(*good_tests)); i++){ aplwc_read(aplwc, (char *)good_tests[i], &line); if((!aplwc_valid_line(&line)) && (aplwc_last_error(aplwc) == APLWC_INVALID_UTF8)) return 1; aplwc_free_line(&line); } aplwc_destroy(aplwc); return 0; }