Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #ifndef HB_OT_NAME_PRIVATE_HH |
| 28 | #define HB_OT_NAME_PRIVATE_HH |
| 29 | |
| 30 | #include "hb-open-type-private.hh" |
| 31 | |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * name |
| 36 | */ |
| 37 | |
| 38 | #define HB_OT_TAG_name HB_TAG('n','a','m','e') |
| 39 | |
| 40 | struct NameRecord |
| 41 | { |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 42 | static int cmp (const NameRecord *a, const NameRecord *b) |
| 43 | { |
| 44 | int ret; |
| 45 | ret = b->platformID.cmp (a->platformID); |
| 46 | if (ret) return ret; |
| 47 | ret = b->encodingID.cmp (a->encodingID); |
| 48 | if (ret) return ret; |
| 49 | ret = b->languageID.cmp (a->languageID); |
| 50 | if (ret) return ret; |
| 51 | ret = b->nameID.cmp (a->nameID); |
| 52 | if (ret) return ret; |
| 53 | return 0; |
| 54 | } |
| 55 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 56 | inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
| 57 | TRACE_SANITIZE (); |
| 58 | /* We can check from base all the way up to the end of string... */ |
| 59 | return c->check_struct (this) && |
| 60 | c->check_range ((char *) base, (unsigned int) length + offset); |
| 61 | } |
| 62 | |
| 63 | USHORT platformID; /* Platform ID. */ |
| 64 | USHORT encodingID; /* Platform-specific encoding ID. */ |
| 65 | USHORT languageID; /* Language ID. */ |
| 66 | USHORT nameID; /* Name ID. */ |
| 67 | USHORT length; /* String length (in bytes). */ |
| 68 | USHORT offset; /* String offset from start of storage area (in bytes). */ |
| 69 | public: |
| 70 | DEFINE_SIZE_STATIC (12); |
| 71 | }; |
| 72 | |
| 73 | struct name |
| 74 | { |
| 75 | static const hb_tag_t Tag = HB_OT_TAG_name; |
| 76 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 77 | inline unsigned int get_name (unsigned int platform_id, |
| 78 | unsigned int encoding_id, |
| 79 | unsigned int language_id, |
| 80 | unsigned int name_id, |
| 81 | void *buffer, |
| 82 | unsigned int buffer_length) const |
| 83 | { |
| 84 | NameRecord key; |
| 85 | key.platformID.set (platform_id); |
| 86 | key.encodingID.set (encoding_id); |
| 87 | key.languageID.set (language_id); |
| 88 | key.nameID.set (name_id); |
| 89 | NameRecord *match = (NameRecord *) bsearch (&key, nameRecord, count, sizeof (nameRecord[0]), (hb_compare_func_t) NameRecord::cmp); |
| 90 | |
| 91 | if (!match) |
| 92 | return 0; |
| 93 | |
| 94 | unsigned int length = MIN (buffer_length, (unsigned int) match->length); |
Behdad Esfahbod | e9c71fa | 2011-08-07 00:00:27 -0400 | [diff] [blame^] | 95 | memcpy (buffer, (char *) this + stringOffset + match->offset, length); |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 96 | return length; |
| 97 | } |
| 98 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 99 | inline bool sanitize_records (hb_sanitize_context_t *c) { |
| 100 | TRACE_SANITIZE (); |
| 101 | unsigned int _count = count; |
| 102 | for (unsigned int i = 0; i < _count; i++) |
| 103 | if (!nameRecord[i].sanitize (c, this + stringOffset)) return false; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | inline bool sanitize (hb_sanitize_context_t *c) { |
| 108 | TRACE_SANITIZE (); |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 109 | return true; |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 110 | return c->check_struct (this) && |
| 111 | likely (format == 0 || format == 1) && |
| 112 | c->check_array (nameRecord, nameRecord[0].static_size, count) && |
| 113 | sanitize_records (c); |
| 114 | } |
| 115 | |
| 116 | /* We only implement format 0 for now. */ |
| 117 | private: |
| 118 | USHORT format; /* Format selector (=0/1). */ |
| 119 | USHORT count; /* Number of name records. */ |
| 120 | Offset stringOffset; /* Offset to start of string storage (from start of table). */ |
| 121 | NameRecord nameRecord[VAR]; /* The name records where count is the number of records. */ |
| 122 | public: |
| 123 | DEFINE_SIZE_ARRAY (6, nameRecord); |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | |
| 128 | #endif /* HB_OT_NAME_PRIVATE_HH */ |