Harald Welte | 3d4869c | 2017-03-07 15:51:49 +0100 | [diff] [blame] | 1 | /* AT91SAM3 USB string descriptor builder |
| 2 | * (C) 2006-2017 by Harald Welte <laforge@gnumonks.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | /* Based on existing utf8_to_utf16le() function, |
| 20 | * Copyright (C) 2003 David Brownell |
| 21 | * |
| 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU Lesser General Public License as published |
| 24 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 25 | * (at your option) any later version. |
| 26 | */ |
| 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #if 0 |
| 36 | static int utf8_to_utf16le(const char *s, uint16_t *cp, unsigned len) |
| 37 | { |
| 38 | int count = 0; |
| 39 | uint8_t c; |
| 40 | uint16_t uchar; |
| 41 | |
| 42 | /* this insists on correct encodings, though not minimal ones. |
| 43 | * BUT it currently rejects legit 4-byte UTF-8 code points, |
| 44 | * which need surrogate pairs. (Unicode 3.1 can use them.) |
| 45 | */ |
| 46 | while (len != 0 && (c = (uint8_t) *s++) != 0) { |
| 47 | if (c & 0x80) { |
| 48 | // 2-byte sequence: |
| 49 | // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx |
| 50 | if ((c & 0xe0) == 0xc0) { |
| 51 | uchar = (c & 0x1f) << 6; |
| 52 | |
| 53 | c = (uint8_t) *s++; |
| 54 | if ((c & 0xc0) != 0xc0) |
| 55 | goto fail; |
| 56 | c &= 0x3f; |
| 57 | uchar |= c; |
| 58 | |
| 59 | // 3-byte sequence (most CJKV characters): |
| 60 | // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx |
| 61 | } else if ((c & 0xf0) == 0xe0) { |
| 62 | uchar = (c & 0x0f) << 12; |
| 63 | |
| 64 | c = (uint8_t) *s++; |
| 65 | if ((c & 0xc0) != 0xc0) |
| 66 | goto fail; |
| 67 | c &= 0x3f; |
| 68 | uchar |= c << 6; |
| 69 | |
| 70 | c = (uint8_t) *s++; |
| 71 | if ((c & 0xc0) != 0xc0) |
| 72 | goto fail; |
| 73 | c &= 0x3f; |
| 74 | uchar |= c; |
| 75 | |
| 76 | /* no bogus surrogates */ |
| 77 | if (0xd800 <= uchar && uchar <= 0xdfff) |
| 78 | goto fail; |
| 79 | |
| 80 | // 4-byte sequence (surrogate pairs, currently rare): |
| 81 | // 11101110wwwwzzzzyy + 110111yyyyxxxxxx |
| 82 | // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx |
| 83 | // (uuuuu = wwww + 1) |
| 84 | // FIXME accept the surrogate code points (only) |
| 85 | |
| 86 | } else |
| 87 | goto fail; |
| 88 | } else |
| 89 | uchar = c; |
| 90 | |
| 91 | *cp++ = uchar; |
| 92 | count++; |
| 93 | len--; |
| 94 | } |
| 95 | return count; |
| 96 | fail: |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | #define COLUMNS 6 |
| 101 | static int print_array16(uint16_t *buf, int len) |
| 102 | { |
| 103 | int i; |
| 104 | for (i = 0; i < len; i++) { |
| 105 | int mod = i % COLUMNS; |
| 106 | char *suffix; |
| 107 | char *prefix; |
| 108 | |
| 109 | switch (mod) { |
| 110 | case 0: |
| 111 | if (i == 0) |
| 112 | prefix = "\t"; |
| 113 | else |
| 114 | prefix= "\t\t\t"; |
| 115 | suffix = ", "; |
| 116 | break; |
| 117 | case COLUMNS-1: |
| 118 | prefix = ""; |
| 119 | suffix = ",\n"; |
| 120 | break; |
| 121 | default: |
| 122 | prefix = ""; |
| 123 | suffix = ", "; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | printf("%s0x%04x%s", prefix, buf[i], suffix); |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | static void print_structhdr(int i, int size) |
| 133 | { |
| 134 | printf("static const unsigned char string%d[] = {\n", i); |
| 135 | printf("\tUSBStringDescriptor_LENGTH(%d),\n", size); |
| 136 | printf("\tUSBGenericDescriptor_STRING,\n"); |
| 137 | } |
| 138 | static void print_structftr(void) |
| 139 | { |
| 140 | printf("};\n\n"); |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char **argv) |
| 144 | { |
| 145 | char asciibuf[512+1]; |
| 146 | uint16_t utf16buf[1024+1]; |
| 147 | int len; |
| 148 | int j, i = 1; |
| 149 | |
| 150 | printf("#pragma once\n\n"); |
| 151 | printf("/* THIS FILE IS AUTOGENERATED, DO NOT MODIFY MANUALLY */\n\n"); |
| 152 | printf("#include \"usb/include/USBDescriptors.h\"\n\n"); |
| 153 | |
| 154 | print_structhdr(0, 1); |
| 155 | printf("\tUSBStringDescriptor_ENGLISH_US\n"); |
| 156 | print_structftr(); |
| 157 | #if 0 |
| 158 | printf("static const struct usb_string_descriptor string0 = {\n" |
| 159 | "\t.bLength = sizeof(string0) + 1 * sizeof(uint16_t),\n" |
| 160 | "\t.bDescriptorType = USB_DT_STRING,\n" |
| 161 | "\t.wData[0] = 0x0409, /* English */\n" |
| 162 | "};\n\n"); |
| 163 | #endif |
| 164 | |
| 165 | while (scanf("%512[^\n]\n", asciibuf) != EOF) { |
| 166 | len = strlen(asciibuf); |
| 167 | printf("/* String %u \"%s\" */\n", i, asciibuf); |
| 168 | print_structhdr(i, len); |
| 169 | |
| 170 | for (j = 0; j < len; j++) |
| 171 | printf("\tUSBStringDescriptor_UNICODE('%c'),\n", asciibuf[j]); |
| 172 | |
| 173 | print_structftr(); |
| 174 | |
| 175 | i++; |
| 176 | } |
| 177 | |
| 178 | printf("const unsigned char *usb_strings[] = {\n"); |
| 179 | for (j = 0; j < i; j++) |
| 180 | printf("\tstring%d,\n", j); |
| 181 | printf("};\n\n"); |
| 182 | |
| 183 | exit(0); |
| 184 | } |