LCOV - code coverage report
Current view: top level - modules/woff2/src - variable_length.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 78 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright 2015 Google Inc. All Rights Reserved.
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : //
      15             : // Helper functions for woff2 variable length types: 255UInt16 and UIntBase128
      16             : 
      17             : #include "./variable_length.h"
      18             : 
      19             : namespace woff2 {
      20             : 
      21           0 : size_t Size255UShort(uint16_t value) {
      22           0 :   size_t result = 3;
      23           0 :   if (value < 253) {
      24           0 :     result = 1;
      25           0 :   } else if (value < 762) {
      26           0 :     result = 2;
      27             :   } else {
      28           0 :     result = 3;
      29             :   }
      30           0 :   return result;
      31             : }
      32             : 
      33           0 : void Write255UShort(std::vector<uint8_t>* out, int value) {
      34           0 :   if (value < 253) {
      35           0 :     out->push_back(value);
      36           0 :   } else if (value < 506) {
      37           0 :     out->push_back(255);
      38           0 :     out->push_back(value - 253);
      39           0 :   } else if (value < 762) {
      40           0 :     out->push_back(254);
      41           0 :     out->push_back(value - 506);
      42             :   } else {
      43           0 :     out->push_back(253);
      44           0 :     out->push_back(value >> 8);
      45           0 :     out->push_back(value & 0xff);
      46             :   }
      47           0 : }
      48             : 
      49           0 : void Store255UShort(int val, size_t* offset, uint8_t* dst) {
      50           0 :   std::vector<uint8_t> packed;
      51           0 :   Write255UShort(&packed, val);
      52           0 :   for (uint8_t val : packed) {
      53           0 :     dst[(*offset)++] = val;
      54             :   }
      55           0 : }
      56             : 
      57             : // Based on section 6.1.1 of MicroType Express draft spec
      58           0 : bool Read255UShort(Buffer* buf, unsigned int* value) {
      59             :   static const int kWordCode = 253;
      60             :   static const int kOneMoreByteCode2 = 254;
      61             :   static const int kOneMoreByteCode1 = 255;
      62             :   static const int kLowestUCode = 253;
      63           0 :   uint8_t code = 0;
      64           0 :   if (!buf->ReadU8(&code)) {
      65           0 :     return FONT_COMPRESSION_FAILURE();
      66             :   }
      67           0 :   if (code == kWordCode) {
      68           0 :     uint16_t result = 0;
      69           0 :     if (!buf->ReadU16(&result)) {
      70           0 :       return FONT_COMPRESSION_FAILURE();
      71             :     }
      72           0 :     *value = result;
      73           0 :     return true;
      74           0 :   } else if (code == kOneMoreByteCode1) {
      75           0 :     uint8_t result = 0;
      76           0 :     if (!buf->ReadU8(&result)) {
      77           0 :       return FONT_COMPRESSION_FAILURE();
      78             :     }
      79           0 :     *value = result + kLowestUCode;
      80           0 :     return true;
      81           0 :   } else if (code == kOneMoreByteCode2) {
      82           0 :     uint8_t result = 0;
      83           0 :     if (!buf->ReadU8(&result)) {
      84           0 :       return FONT_COMPRESSION_FAILURE();
      85             :     }
      86           0 :     *value = result + kLowestUCode * 2;
      87           0 :     return true;
      88             :   } else {
      89           0 :     *value = code;
      90           0 :     return true;
      91             :   }
      92             : }
      93             : 
      94           0 : bool ReadBase128(Buffer* buf, uint32_t* value) {
      95           0 :   uint32_t result = 0;
      96           0 :   for (size_t i = 0; i < 5; ++i) {
      97           0 :     uint8_t code = 0;
      98           0 :     if (!buf->ReadU8(&code)) {
      99           0 :       return FONT_COMPRESSION_FAILURE();
     100             :     }
     101             :     // Leading zeros are invalid.
     102           0 :     if (i == 0 && code == 0x80) {
     103           0 :       return FONT_COMPRESSION_FAILURE();
     104             :     }
     105             :     // If any of the top seven bits are set then we're about to overflow.
     106           0 :     if (result & 0xfe000000) {
     107           0 :       return FONT_COMPRESSION_FAILURE();
     108             :     }
     109           0 :     result = (result << 7) | (code & 0x7f);
     110           0 :     if ((code & 0x80) == 0) {
     111           0 :       *value = result;
     112           0 :       return true;
     113             :     }
     114             :   }
     115             :   // Make sure not to exceed the size bound
     116           0 :   return FONT_COMPRESSION_FAILURE();
     117             : }
     118             : 
     119           0 : size_t Base128Size(size_t n) {
     120           0 :   size_t size = 1;
     121           0 :   for (; n >= 128; n >>= 7) ++size;
     122           0 :   return size;
     123             : }
     124             : 
     125           0 : void StoreBase128(size_t len, size_t* offset, uint8_t* dst) {
     126           0 :   size_t size = Base128Size(len);
     127           0 :   for (size_t i = 0; i < size; ++i) {
     128           0 :     int b = static_cast<int>((len >> (7 * (size - i - 1))) & 0x7f);
     129           0 :     if (i < size - 1) {
     130           0 :       b |= 0x80;
     131             :     }
     132           0 :     dst[(*offset)++] = b;
     133             :   }
     134           0 : }
     135             : 
     136             : } // namespace woff2
     137             : 

Generated by: LCOV version 1.13