/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is WOFF font packaging code.
*
* The Initial Developer of the Original Code is Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
* Bram Stein <b.l.stein@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "woff-private.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "zlib_container.h"
#ifdef WOFF_MOZILLA_CLIENT /* define this when building as part of Gecko */
# include "prmem.h"
# define malloc PR_Malloc
# define realloc PR_Realloc
# define free PR_Free
#endif
/*
* Just simple whole-file encoding and decoding functions; a more extensive
* WOFF library could provide support for accessing individual tables from a
* compressed font, alternative options for memory allocation/ownership and
* error handling, etc.
*/
/* on errors, each function sets a status variable and jumps to failure: */
#undef FAIL
#define FAIL(err) do { status |= err; goto failure; } while (0)
/* adjust an offset for longword alignment */
#define LONGALIGN(x) (((x) + 3) & ~3)
static int
compareOffsets(const void * lhs, const void * rhs)
{
const tableOrderRec * a = (const tableOrderRec *) lhs;
const tableOrderRec * b = (const tableOrderRec *) rhs;
/* don't simply return a->offset - b->offset because these are unsigned
offset values; could convert to int, but possible integer overflow */
return a->offset > b->offset ? 1 :
a->offset < b->offset ? -1 :
0;
}
#ifndef WOFF_MOZILLA_CLIENT
/******************************************************************/
/* * * * * * * * * * * * * * ENCODING * * * * * * * * * * * * * * */
/******************************************************************/
static uint32_t
calcChecksum(const sfntDirEntry * dirEntry,
const uint8_t * sfntData, uint32_t sfntLen)
{
/* just returns zero on errors, they will be detected again elsewhere */
const uint32_t * csumPtr;
const uint32_t * csumEnd;
uint32_t csum = 0;
uint32_t length = LONGALIGN(READ32BE(dirEntry->length));
uint32_t offset = READ32BE(dirEntry->offset);
uint32_t tag;
if ((offset & 3) != 0) {
return csum;
}
if (length > sfntLen || offset > sfntLen - length) {
return csum;
}
csumPtr = (const uint32_t *) (sfntData + offset);
csumEnd = csumPtr + length / 4;
while (csumPtr < csumEnd) {
csum += READ32BE(*csumPtr);
csumPtr++;
|