<commit_before>//=============================================================================
//
// Copyright (C) 1999,2000 by <NAME> <<EMAIL>>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//   2. Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//   3. The name of the author may not be used to endorse or promote products
//      derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
// NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//=============================================================================
//-----------------------------------------------------------------------------
// NeXTKeymap.cpp
//
//	For each keyboard event generated by the host platform, Crystal Space
//	expects two pieces of information: the raw key code, and the cooked
//	character code.  The raw key code is the canonic code generated for
//	that key press with no modifiers applied.  The cooked character code is
//	the actual character generated with all modifiers applied.  Here are
//	some examples:
//
//          Input    Raw  Cooked
//          -------  ---  ------
//          ctrl-C   c    ^C
//          d        d    d
//          shift-e  e    E
//          F        f    F
//          alt-m    m    "mu" *
//          shift-4  4    $
//
//	(*) For alt-m, the cooked character depends upon the user's current key
//	mapping.  It may actually be mapped to any character, but is often
//	mapped to Greek "mu", as it was in this example.
//
//	Unlike OpenStep's -[NSEvent charactersIgnoringModifiers], NextStep's
//	NXEvent does not offer any way of retrieving the raw (unmodified) key
//	code.  Because a cooked character may be generated via an arbitrary
//	combination of modifiers (or none at all), and because a cooked
//	character may even be mapped to more than one keystroke / modifier
//	combination, there is no way to retrieve the raw key code from the
//	cooked character.
//
//	In order to work around this limitation, it is necessary to access the
//	(undocumented) raw keymapping data resource currently in use by the
//	WindowServer and AppKit.  This resource defines the exact mapping
//	between hardware scan codes and the characters which those scan codes
//	generate in the presence of various modifier key combinations.  Among
//	other information, the raw character generated for any given scan code
//	when no modifiers are active can be gleaned from the keymapping
//	resources.
//
//	Using the keymapping resource as its source of information, this class
//	(NeXTKeymap) builds a translation table which maps hardware scan codes
//	to raw key codes (that is, the character mapped to a scan code when no
//	modifiers are active).  Clients of this class can index into the
//	translation table using the hardware scan code which comes from the
//	NXEvent record in order to retrieve the raw key code which Crystal
//	Space expects.  The cooked character code, on the other hand, still
//	comes straight from the NXEvent record.
//
//	The bulk of the code in this file was extracted from the diagnostic
//	tool `dumpkeymap' which was written by <NAME>
//	<<EMAIL>> and is copyright (C)1999,2000 by Eric
//	Sunshine.  The license and copyright notice for dumpkeymap are
//	reproduced above.
//
//	dumpkeymap is a diagnostic utility which interprets and extracts all
//	information contained in an Apple/NeXT .keymapping file and displays it
//	in a human-readable format.  Of greater importance, dumpkeymap contains
//	extensive documentation regarding the exact layout and structure of a
//	keymapping resource.  This is the only known documentation regarding
//	the layout of keymapping resources.
//
//	This file (NeXTKeymap.cpp) makes no attempt to describe the layout of a
//	keymapping resource nor is the actual extraction method explained.  For
//	an exhaustively detailed explanation of each of these topics, refer
//	instead to the original dumpkeymap package.
//
//	The original dumpkeymap package can be found in the directory
//	CS/libs/cssys/next/support/dumpkeymap.  The very latest version of
//	dumpkeymap can also be requested from <NAME> by sending an
//	appropriate mail message to <EMAIL>.  Finally,
//	dumpkeymap is incorporated into the XFree86 (www.xfree86.org)
//	distribution as of release 4.0.2 for the Darwin and MacOS/X ports.
//
//	A fully-detailed description of the internal layout of a keymapping
//	resource can be viewed online as part of the dumpkeymap documentation
//	at http://www.xfree86.org/current/dumpkeymap.1.html.
//
//-----------------------------------------------------------------------------
#include "NeXTKeymap.h"
extern "C" {
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <drivers/event_status_driver.h>
}

typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int natural;
typedef unsigned long dword;
typedef dword number;

#define ASCII_SET 0x00
#define BIND_FUNCTION 0xfe
#define BIND_SPECIAL 0xff

//-----------------------------------------------------------------------------
// Data Stream Object
//	Can be configured to treat embedded "numbers" as being composed of
//	either 1, 2, or 4 bytes, apiece.
//-----------------------------------------------------------------------------
struct DataStream
    {
    byte const* data;
    byte const* data_end;
    natural number_size; // Size in bytes of a "number" in the stream.
    };

static DataStream* new_data_stream( byte const* data, int size )
    {
    DataStream* s = (DataStream*)malloc( sizeof(DataStream) );
    s->data = data;
    s->data_end = data + size;
    s->number_size = 1; // Default to byte-sized numbers.
    return s;
    }

static void destroy_data_stream( DataStream* s )
    {
    free(s);
    }

static int end_of_stream( DataStream* s )
    {
    return (s->data >= s->data_end);
    }

static void expect_nbytes( DataStream* s, int nbytes )
    {
    if (s->data + nbytes > s->data_end)
	{
	fputs( "Insufficient data in keymapping data stream.\n", stderr );
	exit(-1);
	}
    }

static byte get_byte( DataStream* s )
    {
    expect_nbytes( s, 1 );
    return *s->data++;
    }

static word get_word( DataStream* s )
    {
    word hi, lo;
    expect_nbytes( s, 2 );<commit_msg>Eliminated compilation warning in debug mode.<commit_after>//=============================================================================
//
// Copyright (C) 1999,2000 by <NAME> <<EMAIL>>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//   2. Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//   3. The name of the author may not be used to endorse or promote products
//      derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
// NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//=============================================================================
//-----------------------------------------------------------------------------
// NeXTKeymap.cpp
//
//	For each keyboard event generated by the host platform, Crystal Space
//	expects two pieces of information: the raw key code, and the cooked
//	character code.  The raw key code is the canonic code generated for
//	that key press with no modifiers applied.  The cooked character code is
//	the actual character generated with all modifiers applied.  Here are
//	some examples:
//
//          Input    Raw  Cooked
//          -------  ---  ------
//          ctrl-C   c    ^C
//          d        d    d
//          shift-e  e    E
//          F        f    F
//          alt-m    m    "mu" *
//          shift-4  4    $
//
//	(*) For alt-m, the cooked character depends upon the user's current key
//	mapping.  It may actually be mapped to any character, but is often
//	mapped to Greek "mu", as it was in this example.
//
//	Unlike OpenStep's -[NSEvent charactersIgnoringModifiers], NextStep's
//	NXEvent does not offer any way of retrieving the raw (unmodified) key
//	code.  Because a cooked character may be generated via an arbitrary
//	combination of modifiers (or none at all), and because a cooked
//	character may even be mapped to more than one keystroke / modifier
//	combination, there is no way to retrieve the raw key code from the
//	cooked character.
//
//	In order to work around this limitation, it is necessary to access the
//	(undocumented) raw keymapping data resource currently in use by the
//	WindowServer and AppKit.  This resource defines the exact mapping
//	between hardware scan codes and the characters which those scan codes
//	generate in the presence of various modifier key combinations.  Among
//	other information, the raw character generated for any given scan code
//	when no modifiers are active can be gleaned from the keymapping
//	resources.
//
//	Using the keymapping resource as its source of information, this class
//	(NeXTKeymap) builds a translation table which maps hardware scan codes
//	to raw key codes (that is, the character mapped to a scan code when no
//	modifiers are active).  Clients of this class can index into the
//	translation table using the hardware scan code which comes from the
//	NXEvent record in order to retrieve the raw key code which Crystal
//	Space expects.  The cooked character code, on the other hand, still
//	comes straight from the NXEvent record.
//
//	The bulk of the code in this file was extracted from the diagnostic
//	tool `dumpkeymap' which was written by <NAME>
//	<<EMAIL>> and is copyright (C)1999,2000 by Eric
//	Sunshine.  The license and copyright notice for dumpkeymap are
//	reproduced above.
//
//	dumpkeymap is a diagnostic utility which interprets and extracts all
//	information contained in an Apple/NeXT .keymapping file and displays it
//	in a human-readable format.  Of greater importance, dumpkeymap contains
//	extensive documentation regarding the exact layout and structure of a
//	keymapping resource.  This is the only known documentation regarding
//	the layout of keymapping resources.
//
//	This file (NeXTKeymap.cpp) makes no attempt to describe the layout of a
//	keymapping resource nor is the actual extraction method explained.  For
//	an exhaustively detailed explanation of each of these topics, refer
//	instead to the original dumpkeymap package.
//
//	The original dumpkeymap package can be found in the directory
//	CS/libs/cssys/next/support/dumpkeymap.  The very latest version of
//	dumpkeymap can also be requested from <NAME> by sending an
//	appropriate mail message to <EMAIL>.  Finally,
//	dumpkeymap is incorporated into the XFree86 (www.xfree86.org)
//	distribution as of release 4.0.2 for the Darwin and MacOS/X ports.
//
//	A fully-detailed description of the internal layout of a keymapping
//	resource can be viewed online as part of the dumpkeymap documentation
//	at http://www.xfree86.org/current/dumpkeymap.1.html.
//
//-----------------------------------------------------------------------------
#include "NeXTKeymap.h"
extern "C" {
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <drivers/event_status_driver.h>
}

typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int natural;
typedef unsigned long dword;
typedef dword number;

#define ASCII_SET 0x00
#define BIND_FUNCTION 0xfe
#define BIND_SPECIAL 0xff

//-----------------------------------------------------------------------------
// Data Stream Object
//	Can be configured to treat embedded "numbers" as being composed of
//	either 1, 2, or 4 bytes, apiece.
//-----------------------------------------------------------------------------
struct DataStream
    {
    byte const* data;
    byte const* data_end;
    natural number_size; // Size in bytes of a "number" in the stream.
    };

static DataStream* new_data_stream( byte const* data, int size )
    {
    DataStream* s = (DataStream*)malloc( sizeof(DataStream) );
    s->data = data;
    s->data_end = data + size;
    s->number_size = 1; // Default to byte-sized numbers.
    return s;
    }

static void destroy_data_stream( DataStream* s )
    {
    free(s);
    }

#if defined(END_OF_STREAM_USED)
static int end_of_stream( DataStream* s )
    {
    return (s->data >= s->data_end);
    }
#endif

static void expect_nbytes( DataStream* s, int nbytes )
    {
    if (s->data + nbytes > s->data_end)
	{
	fputs( "Insufficient data in keymapping data stream.\n", stderr );
	exit(-1);
	}
    }

static byte get_byte( DataStream* s )
    {
    expect_nbytes( s, 1 );
    return *s->data++;
    }

static word get_word( DataStream* s )
    {
    word hi, lo;
    expect_nbytes( s, 2 );
{'fraction_non_alphanumeric': 0.14104038422512347, 'fraction_numerical': 0.005817493066360008, 'mean_word_length': 2.696, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7136100', 'n_tokens_mistral': 4088, 'n_tokens_neox': 3835, 'n_words': 1844}