1#!/usr/bin/env python3 2 3""" 4string to string mapping table 5 6License: MIT 7 8Copyright (c) 2020 Simon Ser 9Copyright 2022 Collabora, Ltd. 10 11Permission is hereby granted, free of charge, to any person obtaining a copy 12of this software and associated documentation files (the "Software"), to deal 13in the Software without restriction, including without limitation the rights 14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15copies of the Software, and to permit persons to whom the Software is 16furnished to do so, subject to the following conditions: 17 18The above copyright notice and this permission notice shall be included in all 19copies or substantial portions of the Software. 20 21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27SOFTWARE. 28""" 29 30import sys 31 32def escape_for_c(s): 33 l = [c if c.isalnum() or c in ' .,' else '\\%03o' % ord(c) for c in s] 34 return ''.join(l) 35 36if len(sys.argv) != 4: 37 print('usage: ' + sys.argv[0] + ' <infile> <outfile> <ident>', file=sys.stderr) 38 sys.exit(1) 39 40infile = sys.argv[1] 41outfile = sys.argv[2] 42ident = sys.argv[3] 43 44records = {} 45 46with open(infile, 'r') as f: 47 for line in f: 48 [pnpid, name] = line.split(maxsplit=1) 49 50 if len(pnpid) != 3: 51 print("Warning: skipping invalid PNP ID %s" % (repr(pnpid)), file=sys.stderr) 52 continue 53 54 records[pnpid] = escape_for_c(name.strip()) 55 56with open(outfile, 'w') as f: 57 f.write( 58f''' 59#include <string.h> 60#include <stdint.h> 61 62const char * 63{ident}(const char *key); 64 65const char * 66{ident}(const char *key) 67{{ 68 size_t len = strlen(key); 69 size_t i; 70 uint32_t u = 0; 71 72 if (len > 4) 73 return NULL; 74 75 for (i = 0; i < len; i++) 76 u = (u << 8) | (uint8_t)key[i]; 77 78 switch (u) {{ 79''') 80 for id in sorted(records.keys()): 81 u = 0 82 for c in id: 83 u = (u << 8) | ord(c) 84 f.write(f' case {u}: return "{records[id]}";\n') 85 f.write( 86f''' 87 default: 88 return NULL; 89 }} 90}} 91''') 92