1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 /*
  31  * Copyright © 2011,2012  Google, Inc.
  32  *
  33  *  This is part of HarfBuzz, a text shaping library.
  34  *
  35  * Permission is hereby granted, without written agreement and without
  36  * license or royalty fees, to use, copy, modify, and distribute this
  37  * software and its documentation for any purpose, provided that the
  38  * above copyright notice and the following two paragraphs appear in
  39  * all copies of this software.
  40  *
  41  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  42  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  43  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  44  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  45  * DAMAGE.
  46  *
  47  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  48  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  49  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  50  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  52  *
  53  * Google Author(s): Behdad Esfahbod
  54  */
  55 
  56 #ifndef HB_OT_NAME_TABLE_HH
  57 #define HB_OT_NAME_TABLE_HH
  58 
  59 #include "hb-open-type-private.hh"
  60 
  61 
  62 namespace OT {
  63 
  64 
  65 /*
  66  * name -- The Naming Table
  67  */
  68 
  69 #define HB_OT_TAG_name HB_TAG('n','a','m','e')
  70 
  71 
  72 struct NameRecord
  73 {
  74   static int cmp (const NameRecord *a, const NameRecord *b)
  75   {
  76     int ret;
  77     ret = b->platformID.cmp (a->platformID);
  78     if (ret) return ret;
  79     ret = b->encodingID.cmp (a->encodingID);
  80     if (ret) return ret;
  81     ret = b->languageID.cmp (a->languageID);
  82     if (ret) return ret;
  83     ret = b->nameID.cmp (a->nameID);
  84     if (ret) return ret;
  85     return 0;
  86   }
  87 
  88   inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
  89   {
  90     TRACE_SANITIZE (this);
  91     /* We can check from base all the way up to the end of string... */
  92     return_trace (c->check_struct (this) && c->check_range ((char *) base, (unsigned int) length + offset));
  93   }
  94 
  95   USHORT        platformID;     /* Platform ID. */
  96   USHORT        encodingID;     /* Platform-specific encoding ID. */
  97   USHORT        languageID;     /* Language ID. */
  98   USHORT        nameID;         /* Name ID. */
  99   USHORT        length;         /* String length (in bytes). */
 100   USHORT        offset;         /* String offset from start of storage area (in bytes). */
 101   public:
 102   DEFINE_SIZE_STATIC (12);
 103 };
 104 
 105 struct name
 106 {
 107   static const hb_tag_t tableTag        = HB_OT_TAG_name;
 108 
 109   inline unsigned int get_name (unsigned int platform_id,
 110                                 unsigned int encoding_id,
 111                                 unsigned int language_id,
 112                                 unsigned int name_id,
 113                                 void *buffer,
 114                                 unsigned int buffer_length) const
 115   {
 116     NameRecord key;
 117     key.platformID.set (platform_id);
 118     key.encodingID.set (encoding_id);
 119     key.languageID.set (language_id);
 120     key.nameID.set (name_id);
 121     NameRecord *match = (NameRecord *) bsearch (&key, nameRecord, count, sizeof (nameRecord[0]), (hb_compare_func_t) NameRecord::cmp);
 122 
 123     if (!match)
 124       return 0;
 125 
 126     unsigned int length = MIN (buffer_length, (unsigned int) match->length);
 127     memcpy (buffer, (char *) this + stringOffset + match->offset, length);
 128     return length;
 129   }
 130 
 131   inline unsigned int get_size (void) const
 132   { return min_size + count * nameRecord[0].min_size; }
 133 
 134   inline bool sanitize_records (hb_sanitize_context_t *c) const {
 135     TRACE_SANITIZE (this);
 136     char *string_pool = (char *) this + stringOffset;
 137     unsigned int _count = count;
 138     for (unsigned int i = 0; i < _count; i++)
 139       if (!nameRecord[i].sanitize (c, string_pool)) return_trace (false);
 140     return_trace (true);
 141   }
 142 
 143   inline bool sanitize (hb_sanitize_context_t *c) const
 144   {
 145     TRACE_SANITIZE (this);
 146     return_trace (c->check_struct (this) &&
 147                   likely (format == 0 || format == 1) &&
 148                   c->check_array (nameRecord, nameRecord[0].static_size, count) &&
 149                   sanitize_records (c));
 150   }
 151 
 152   /* We only implement format 0 for now. */
 153   USHORT        format;                 /* Format selector (=0/1). */
 154   USHORT        count;                  /* Number of name records. */
 155   Offset<>      stringOffset;           /* Offset to start of string storage (from start of table). */
 156   NameRecord    nameRecord[VAR];        /* The name records where count is the number of records. */
 157   public:
 158   DEFINE_SIZE_ARRAY (6, nameRecord);
 159 };
 160 
 161 
 162 } /* namespace OT */
 163 
 164 
 165 #endif /* HB_OT_NAME_TABLE_HH */