code:
/*-
 * Copyright (c) 1997, 1998, 1999
 *	Bill Paul <wpaul@ctr.columbia.edu>.  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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``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 Bill Paul OR THE VOICES IN HIS HEAD
 * 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.
 *
 * $FreeBSD: release/9.0.0/sys/dev/wi/if_wireg.h 192468 2009-05-20 20:00:40Z sam $
 */

#define WI_DELAY	5
#define WI_TIMEOUT	(500000/WI_DELAY)	/* 500 ms */

#define WI_PORT0	(0 << 8)
#define WI_PORT1	(1 << 8)
#define WI_PORT2	(2 << 8)
#define WI_PORT3	(3 << 8)
#define WI_PORT4	(4 << 8)
#define WI_PORT5	(5 << 8)

#define WI_PCI_LMEMRES	0x10	/* PCI Memory (native PCI implementations) */
#define WI_PCI_LOCALRES	0x14	/* The PLX chip's local registers */
#define WI_PCI_MEMRES	0x18	/* The PCCard's attribute memory */
#define WI_PCI_IORES	0x1C	/* The PCCard's I/O space */

#define WI_LOCAL_INTCSR		0x4c
#define WI_LOCAL_INTEN		0x40
#define WI_HFA384X_SWSUPPORT0_OFF	0x28
#define WI_PRISM2STA_MAGIC		0x4A2D
#define WI_PCICOR_OFF		0x26
#define WI_PCICOR_RESET		0x0080

/* Default port: 0 (only 0 exists on stations) */
#define WI_DEFAULT_PORT	WI_PORT0

/* Default network name: ANY */
/*
 * [sommerfeld 1999/07/15] Changed from "ANY" to ""; according to Bill Fenner,
 * ANY is used in MS driver user interfaces, while "" is used over the
 * wire..
 */
#define WI_DEFAULT_NETNAME	""

#define WI_DEFAULT_AP_DENSITY	1

#define WI_DEFAULT_RTS_THRESH	2347

#define WI_DEFAULT_DATALEN	2304

#define WI_DEFAULT_CREATE_IBSS	0

#define WI_DEFAULT_PM_ENABLED	0

#define WI_DEFAULT_MAX_SLEEP	100

#define WI_DEFAULT_ROAMING	1

#define WI_DEFAULT_AUTHTYPE	1

#ifdef __NetBSD__
#define OS_STRING_NAME	"NetBSD"
#endif
#ifdef __FreeBSD__
#define OS_STRING_NAME	"FreeBSD"
#endif
#ifdef __OpenBSD__
#define OS_STRING_NAME	"OpenBSD"
#endif

#define WI_DEFAULT_NODENAME	OS_STRING_NAME " WaveLAN/IEEE node"

#define WI_DEFAULT_IBSS		OS_STRING_NAME " IBSS"

#define WI_BUS_PCCARD		0	/* pccard device */
#define WI_BUS_PCI_PLX		1	/* PCI card w/ PLX PCI/PCMICA bridge */
#define WI_BUS_PCI_NATIVE	2	/* native PCI device (Prism 2.5) */

/*
 * register space access macros
 */
#define CSR_WRITE_4(sc, reg, val)				\
	bus_space_write_4((sc)->wi_btag, (sc)->wi_bhandle, 	\
	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg), val)
#define CSR_WRITE_2(sc, reg, val)				\
	bus_space_write_2((sc)->wi_btag, (sc)->wi_bhandle,	\
 	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg), val)
#define CSR_WRITE_1(sc, reg, val)				\
	bus_space_write_1((sc)->wi_btag, (sc)->wi_bhandle,	\
 	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg), val)

#define CSR_READ_4(sc, reg)					\
	bus_space_read_4((sc)->wi_btag, (sc)->wi_bhandle,	\
 	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg))
#define CSR_READ_2(sc, reg)					\
	bus_space_read_2((sc)->wi_btag, (sc)->wi_bhandle,	\
 	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg))
#define CSR_READ_1(sc, reg)					\
	bus_space_read_1((sc)->wi_btag, (sc)->wi_bhandle,	\
 	    (sc)->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg)*2 : (reg))

#define CSM_WRITE_1(sc, off, val)	\
	bus_space_write_1((sc)->wi_bmemtag, (sc)->wi_bmemhandle, off, val)

#define CSM_READ_1(sc, off)		\
	bus_space_read_1((sc)->wi_bmemtag, (sc)->wi_bmemhandle, off)

#define CSR_WRITE_STREAM_2(sc, reg, val)	\
	bus_space_write_stream_2(sc->wi_btag, sc->wi_bhandle,	\
	    (sc->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg) * 2 : (reg)), val)
#define CSR_WRITE_MULTI_STREAM_2(sc, reg, val, count)	\
	bus_space_write_multi_stream_2(sc->wi_btag, sc->wi_bhandle,	\
	    (sc->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg) * 2 : (reg)), val, count)
#define CSR_READ_STREAM_2(sc, reg)		\
	bus_space_read_stream_2(sc->wi_btag, sc->wi_bhandle,	\
	    (sc->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg) * 2 : (reg)))
#define CSR_READ_MULTI_STREAM_2(sc, reg, buf, count)		\
	bus_space_read_multi_stream_2(sc->wi_btag, sc->wi_bhandle,	\
	    (sc->wi_bus_type == WI_BUS_PCI_NATIVE ? (reg) * 2 : (reg)), buf, count)

/*
 * The WaveLAN/IEEE cards contain an 802.11 MAC controller which Lucent
 * calls 'Hermes.' In typical fashion, getting documentation about this
 * controller is about as easy as squeezing blood from a stone. Here
 * is more or less what I know:
 *
 * - The Hermes controller is firmware driven, and the host interacts
 *   with the Hermes via a firmware interface, which can change.
 *
 * - The Hermes is described in a document called: "Hermes Firmware
 *   WaveLAN/IEEE Station Functions," document #010245, which of course
 *   Lucent will not release without an NDA.
 *
 * - Lucent has created a library called HCF (Hardware Control Functions)
 *   though which it wants developers to interact with the card. The HCF
 *   is needlessly complex, ill conceived and badly documented. Actually,
 *   the comments in the HCP code itself aren't bad, but the publically
 *   available manual that comes with it is awful, probably due largely to
 *   the fact that it has been emasculated in order to hide information
 *   that Lucent wants to keep proprietary. The purpose of the HCF seems
 *   to be to insulate the driver programmer from the Hermes itself so that
 *   Lucent has an excuse not to release programming in for it.
 *
 * - Lucent only makes available documentation and code for 'HCF Light'
 *   which is a stripped down version of HCF with certain features not
 *   implemented, most notably support for 802.11 frames.
 *
 * - The HCF code which I have seen blows goats. Whoever decided to
 *   use a 132 column format should be shot.
 *
 * Rather than actually use the Lucent HCF library, I have stripped all
 * the useful information from it and used it to create a driver in the
 * usual BSD form. Note: I don't want to hear anybody whining about the
 * fact that the Lucent code is GPLed and mine isn't. I did not actually
 * put any of Lucent's code in this driver: I only used it as a reference
 * to obtain information about the underlying hardware. The Hermes
 * programming interface is not GPLed, so bite me.
 */

/*
 * Size of Hermes & Prism2 I/O space.
 */
#define WI_IOSIZ		0x40

/*
 * Hermes & Prism2 register definitions
 */

/* Hermes command/status registers. */
#define WI_COMMAND		0x00
#define WI_PARAM0		0x02
#define WI_PARAM1		0x04
#define WI_PARAM2		0x06
#define WI_STATUS		0x08
#define WI_RESP0		0x0A
#define WI_RESP1		0x0C
#define WI_RESP2		0x0E

/* Command register values. */
#define WI_CMD_BUSY		0x8000 /* busy bit */
#define WI_CMD_INI		0x0000 /* initialize */
#define WI_CMD_ENABLE		0x0001 /* enable */
#define WI_CMD_DISABLE		0x0002 /* disable */
#define WI_CMD_DIAG		0x0003
#define WI_CMD_ALLOC_MEM	0x000A /* allocate NIC memory */
#define WI_CMD_TX		0x000B /* transmit */
#define WI_CMD_NOTIFY		0x0010
#define WI_CMD_INQUIRE		0x0011
#define WI_CMD_ACCESS		0x0021
#define WI_CMD_ACCESS_WRITE	0x0121
#define WI_CMD_PROGRAM		0x0022
#define WI_CMD_READEE		0x0030	/* symbol only */
#define WI_CMD_READMIF		0x0030	/* prism2 */
#define WI_CMD_WRITEMIF		0x0031	/* prism2 */
#define WI_CMD_DEBUG		0x0038	/* Various test commands */

#define WI_CMD_CODE_MASK	0x003F

/*
 * Various cmd test stuff.
 */
#define WI_TEST_MONITOR		0x0B
#define WI_TEST_STOP		0x0F
#define WI_TEST_CFG_BITS	0x15
#define WI_TEST_CFG_BIT_ALC	0x08

/*
 * Reclaim qualifier bit, applicable to the
 * TX and INQUIRE commands.
 */
#define WI_RECLAIM		0x0100 /* reclaim NIC memory */

/*
 * ACCESS command qualifier bits.
 */
#define WI_ACCESS_READ		0x0000
#define WI_ACCESS_WRITE		0x0100

/*
 * PROGRAM command qualifier bits.
 */
#define WI_PROGRAM_DISABLE	0x0000
#define WI_PROGRAM_ENABLE_RAM	0x0100
#define WI_PROGRAM_ENABLE_NVRAM	0x0200
#define WI_PROGRAM_NVRAM	0x0300

/* Status register values */
#define WI_STAT_CMD_CODE	0x003F
#define WI_STAT_DIAG_ERR	0x0100
#define WI_STAT_INQ_ERR		0x0500
#define WI_STAT_CMD_RESULT	0x7F00

/* memory handle management registers */
#define WI_INFO_FID		0x10
#define WI_RX_FID		0x20
#define WI_ALLOC_FID		0x22
#define WI_TX_CMP_FID		0x24

/*
 * Buffer Access Path (BAP) registers.
 * These are I/O channels. I believe you can use each one for
 * any desired purpose independently of the other. In general
 * though, we use BAP1 for reading and writing LTV records and
 * reading received data frames, and BAP0 for writing transmit
 * frames. This is a convention though, not a rule.
 */
#define WI_SEL0			0x18
#define WI_SEL1			0x1A
#define WI_OFF0			0x1C
#define WI_OFF1			0x1E
#define WI_DATA0		0x36
#define WI_DATA1		0x38
#define WI_BAP0			WI_DATA0
#define WI_BAP1			WI_DATA1

#define WI_OFF_BUSY		0x8000
#define WI_OFF_ERR		0x4000
#define WI_OFF_DATAOFF		0x0FFF

/* Event registers */
#define WI_EVENT_STAT		0x30	/* Event status */
#define WI_INT_EN		0x32	/* Interrupt enable/disable */
#define WI_EVENT_ACK		0x34	/* Ack event */

/* Events */
#define WI_EV_TICK		0x8000	/* aux timer tick */
#define WI_EV_RES		0x4000	/* controller h/w error (time out) */
#define WI_EV_INFO_DROP		0x2000	/* no RAM to build unsolicited frame */
#define WI_EV_NO_CARD		0x0800	/* card removed (hunh?) */
#define WI_EV_DUIF_RX		0x0400	/* wavelan management packet received */
#define WI_EV_INFO		0x0080	/* async info frame */
#define WI_EV_CMD		0x0010	/* command completed */
#define WI_EV_ALLOC		0x0008	/* async alloc/reclaim completed */
#define WI_EV_TX_EXC		0x0004	/* async xmit completed with failure */
#define WI_EV_TX		0x0002	/* async xmit completed succesfully */
#define WI_EV_RX		0x0001	/* async rx completed */

/* Host software registers */
#define WI_SW0			0x28
#define WI_SW1			0x2A
#define WI_SW2			0x2C
#define WI_SW3			0x2E 	/* does not appear in Prism2 */

#define WI_CNTL			0x14

#define WI_CNTL_AUX_ENA		0xC000
#define WI_CNTL_AUX_ENA_STAT	0xC000
#define WI_CNTL_AUX_DIS_STAT	0x0000
#define WI_CNTL_AUX_ENA_CNTL	0x8000
#define WI_CNTL_AUX_DIS_CNTL	0x4000

#define WI_AUX_PAGE		0x3A
#define WI_AUX_OFFSET		0x3C
#define WI_AUX_DATA		0x3E

#define WI_AUX_PGSZ		128
#define WI_AUX_KEY0		0xfe01
#define WI_AUX_KEY1		0xdc23
#define WI_AUX_KEY2		0xba45

#define WI_COR			0x40	/* only for Symbol */
#define WI_COR_RESET		0x0080
#define WI_COR_IOMODE		0x0041

#define WI_HCR			0x42	/* only for Symbol */
#define WI_HCR_4WIRE		0x0010
#define WI_HCR_RUN		0x0007
#define WI_HCR_HOLD		0x000f
#define WI_HCR_EEHOLD		0x00ce

#define WI_COR_OFFSET	0x3e0	/* OK for PCI, default COR for Prism PC Card */
#define WI_COR_VALUE	0x41

/*
 * One form of communication with the Hermes is with what Lucent calls
 * LTV records, where LTV stands for Length, Type and Value. The length
 * and type are 16 bits and are in native byte order. The value is in
 * multiples of 16 bits and is in little endian byte order.
 */
struct wi_lt_hdr {
	u_int16_t		wi_len;
	u_int16_t		wi_type;
	/* value is vary depends on resource id */
};

/*
 * Download buffer location and length (0xFD01).
 */
struct wi_dnld_buf {
	u_int16_t		wi_buf_pg; /* page addr of intermediate dl buf*/
	u_int16_t		wi_buf_off; /* offset of idb */
	u_int16_t		wi_buf_len; /* len of idb */
};

/*
 * Mem sizes (0xFD02).
 */
struct wi_memsz {
	u_int16_t		wi_mem_ram;
	u_int16_t		wi_mem_nvram;
};

/*
 * NIC Identification (0xFD0B, 0xFD20)
 */
struct wi_ver {
	u_int16_t		wi_ver[4];
};

/* define card ident */
#define	WI_NIC_LUCENT_ID	0x0001
#define	WI_NIC_LUCENT_STR	"Lucent Technologies, WaveLAN/IEEE"

#define	WI_NIC_SONY_ID		0x0002
#define	WI_NIC_SONY_STR		"Sony WaveLAN/IEEE"

#define	WI_NIC_LUCENT_EMB_ID	0x0005
#define	WI_NIC_LUCENT_EMB_STR	"Lucent Embedded WaveLAN/IEEE"

#define	WI_NIC_EVB2_ID		0x8000
#define	WI_NIC_EVB2_STR		"RF:PRISM2 MAC:HFA3841"

#define	WI_NIC_HWB3763_ID	0x8001
#define	WI_NIC_HWB3763_STR	"RF:PRISM2 MAC:HFA3841 CARD:HWB3763 rev.B"

#define	WI_NIC_HWB3163_ID	0x8002
#define	WI_NIC_HWB3163_STR	"RF:PRISM2 MAC:HFA3841 CARD:HWB3163 rev.A"

#define	WI_NIC_HWB3163B_ID	0x8003
#define	WI_NIC_HWB3163B_STR	"RF:PRISM2 MAC:HFA3841 CARD:HWB3163 rev.B"

#define	WI_NIC_EVB3_ID		0x8004
#define	WI_NIC_EVB3_STR		"RF:PRISM2 MAC:HFA3842 CARD:HFA3842 EVAL"

#define	WI_NIC_HWB1153_ID	0x8007
#define	WI_NIC_HWB1153_STR	"RF:PRISM1 MAC:HFA3841 CARD:HWB1153"

#define	WI_NIC_P2_SST_ID	0x8008	/* Prism2 with SST flush */
#define	WI_NIC_P2_SST_STR	"RF:PRISM2 MAC:HFA3841 CARD:HWB3163-SST-flash"

#define	WI_NIC_EVB2_SST_ID	0x8009
#define	WI_NIC_EVB2_SST_STR	"RF:PRISM2 MAC:HFA3841 CARD:HWB3163-SST-flash"

#define	WI_NIC_3842_EVA_ID	0x800A	/* 3842 Evaluation Board */
#define	WI_NIC_3842_EVA_STR	"RF:PRISM2 MAC:HFA3842 CARD:HFA3842 EVAL"

#define	WI_NIC_3842_PCMCIA_AMD_ID	0x800B	/* Prism2.5 PCMCIA */
#define	WI_NIC_3842_PCMCIA_SST_ID	0x800C
#define	WI_NIC_3842_PCMCIA_ATL_ID	0x800D
#define	WI_NIC_3842_PCMCIA_ATS_ID	0x800E
#define	WI_NIC_3842_PCMCIA_STR		"RF:PRISM2.5 MAC:ISL3873"

#define	WI_NIC_3842_MINI_AMD_ID		0x8012	/* Prism2.5 Mini-PCI */
#define	WI_NIC_3842_MINI_SST_ID		0x8013
#define	WI_NIC_3842_MINI_ATL_ID		0x8014
#define	WI_NIC_3842_MINI_ATS_ID		0x8015
#define	WI_NIC_3842_MINI_STR		"RF:PRISM2.5 MAC:ISL3874A(Mini-PCI)"

#define	WI_NIC_3842_PCI_AMD_ID		0x8016	/* Prism2.5 PCI-bridge */
#define	WI_NIC_3842_PCI_SST_ID		0x8017
#define	WI_NIC_3842_PCI_ATL_ID		0x8018
#define	WI_NIC_3842_PCI_ATS_ID		0x8019
#define	WI_NIC_3842_PCI_STR		"RF:PRISM2.5 MAC:ISL3874A(PCI-bridge)"

#define	WI_NIC_P3_PCMCIA_AMD_ID		0x801A	/* Prism3 PCMCIA */
#define	WI_NIC_P3_PCMCIA_SST_ID		0x801B
#define	WI_NIC_P3_PCMCIA_ATL_ID		0x801C
#define	WI_NIC_P3_PCMCIA_ATS_ID		0x801D
#define	WI_NIC_P3_PCMCIA_STR		"RF:PRISM3(PCMCIA)"

#define WI_NIC_P3_USB_AMD_ID		0x801E
#define WI_NIC_P3_USB_SST_ID		0x801F
#define	WI_NIC_P3_USB_ATL_ID		0x8020

#define	WI_NIC_P3_MINI_AMD_ID		0x8021	/* Prism3 Mini-PCI */
#define	WI_NIC_P3_MINI_SST_ID		0x8022
#define	WI_NIC_P3_MINI_ATL_ID		0x8023
#define	WI_NIC_P3_MINI_ATS_ID		0x8024
#define	WI_NIC_P3_MINI_STR		"RF:PRISM3(Mini-PCI)"

/*
 * List of intended regulatory domains (0xFD11).
 */
struct wi_ltv_domains {
	u_int16_t		wi_len;
	u_int16_t		wi_type;
	u_int16_t		wi_num_dom;
	u_int8_t		wi_domains[10];
};

/*
 * CIS struct (0xFD13).
 */
struct wi_cis {
	u_int16_t		wi_cis[240];
};

/*
 * Communications quality (0xFD43).
 */
struct wi_commqual {
	u_int16_t		wi_coms_qual;
	u_int16_t		wi_sig_lvl;
	u_int16_t		wi_noise_lvl;
};

/*
 * Actual system scale thresholds (0xFC06, 0xFD46).
 */
struct wi_scalethresh {
	u_int16_t		wi_energy_detect;
	u_int16_t		wi_carrier_detect;
	u_int16_t		wi_defer;
	u_int16_t		wi_cell_search;
	u_int16_t		wi_out_of_range;
	u_int16_t		wi_delta_snr;
};

/*
 * PCF info struct (0xFD87).
 */
struct wi_pcf {
	u_int16_t		wi_medium_occupancy_limit;
	u_int16_t		wi_cfp_period;
	u_int16_t		wi_cfp_max_duration;
};

/*
 * Connection control characteristics. (0xFC00)
 * 0 == IBSS (802.11 compliant mode) (Only PRISM2)
 * 1 == Basic Service Set (BSS)
 * 2 == Wireless Distribudion System (WDS)
 * 3 == Pseudo IBSS
 *	(Only PRISM2; not 802.11 compliant mode, testing use only)
 * 6 == HOST AP (Only PRISM2)
 */
#define	WI_PORTTYPE_IBSS	0x0
#define WI_PORTTYPE_BSS		0x1
#define WI_PORTTYPE_WDS		0x2
#define WI_PORTTYPE_ADHOC	0x3
#define WI_PORTTYPE_APSILENT	0x5
#define	WI_PORTTYPE_HOSTAP	0x6

/*
 * Mac addresses. (0xFC01, 0xFC08)
 */
struct wi_macaddr {
	u_int8_t		wi_mac_addr[6];
};

/*
 * Station set identification (SSID). (0xFC02, 0xFC04)
 */
struct wi_ssid {
	u_int16_t		wi_len;
	u_int8_t		wi_ssid[32];
};

/*
 * Set our station name. (0xFC0E)
 */
struct wi_nodename {
	u_int16_t		wi_nodelen;
	u_int8_t		wi_nodename[32];
};

/*
 * Multicast addresses to be put in filter. We're
 * allowed up to 16 addresses in the filter. (0xFC80)
 */
struct wi_mcast {
	struct ether_addr	wi_mcast[16];
};

/*
 * Join request. (0xFCE2)
 */
struct wi_joinreq {
	struct ether_addr	wi_bssid;
	u_int16_t		wi_chan;
};

/*
 * supported rates. (0xFCB4)
 */
#define WI_SUPPRATES_1M		0x0001
#define WI_SUPPRATES_2M		0x0002
#define WI_SUPPRATES_5M		0x0004
#define WI_SUPPRATES_11M	0x0008
#define	WI_RATES_BITS	"\20\0011M\0022M\0035.5M\00411M"

/*
 * Information frame types.
 */
#define WI_INFO_NOTIFY		0xF000	/* Handover address */
#define WI_INFO_COUNTERS	0xF100	/* Statistics counters */
#define WI_INFO_SCAN_RESULTS	0xF101	/* Scan results */
#define WI_INFO_HOST_SCAN_RESULTS	0xF104	/* Scan results */
#define WI_INFO_LINK_STAT	0xF200	/* Link status */
#define	WI_INFO_LINK_STAT_CONNECTED	1
#define	WI_INFO_LINK_STAT_DISCONNECTED	2
#define	WI_INFO_LINK_STAT_AP_CHG	3	/* AP Change */
#define	WI_INFO_LINK_STAT_AP_OOR	4	/* AP Out Of Range */
#define	WI_INFO_LINK_STAT_AP_INR	5	/* AP In Range */
#define	WI_INFO_LINK_STAT_ASSOC_FAILED	6
#define WI_INFO_ASSOC_STAT	0xF201	/* Association status */
#define	WI_INFO_AUTH_REQUEST	0xF202	/* Authentication Request (AP) */
#define	WI_INFO_POWERSAVE_COUNT	0xF203	/* PowerSave User Count (AP) */

struct wi_assoc {
	u_int16_t		wi_assoc_stat;	/* Association Status */
#define	ASSOC		1
#define	REASSOC		2
#define	DISASSOC	3
#define	ASSOCFAIL	4
#define	AUTHFAIL	5
	u_int8_t		wi_assoc_sta[6];	/* Station Address */
	u_int8_t		wi_assoc_osta[6];	/* OLD Station Address */
	u_int16_t		wi_assoc_reason;	/* Reason */
	u_int16_t		wi_assoc_reserve;	/* Reserved */
};

/*
 * Scan Results of Prism2 chip
 */

struct wi_scan_header {
	u_int16_t		wi_reserve;	/* future use */
	u_int16_t		wi_reason;	/* The reason this scan was initiated
						   1: Host initiated
						   2: Firmware initiated
						   3: Inquiry request from host */
};

struct wi_scan_data_p2 {
	u_int16_t		wi_chid;	/* BSS Channel ID from Probe Res.(PR)*/
	u_int16_t		wi_noise;	/* Average Noise Level of the PR */
	u_int16_t		wi_signal;	/* Signal Level on the PR */
	u_int8_t		wi_bssid[6];	/* MACaddress of BSS responder from PR */
	u_int16_t		wi_interval;	/* BSS beacon interval */
	u_int16_t		wi_capinfo;	/* BSS Capability Information
						   IEEE Std 802.11(1997) ,see 7.3.1.4 */
	u_int16_t		wi_namelen;	/* Length of SSID strings */
	u_int8_t		wi_name[32];	/* SSID strings */
	u_int16_t		wi_suprate[5];	/* Supported Rates element from the PR
						   IEEE Std 802.11(1997) ,see 7.3.2.2 */
	u_int16_t		wi_rate;	/* Data rate of the PR */
#define	WI_APRATE_1		0x0A		/* 1 Mbps */
#define	WI_APRATE_2		0x14		/* 2 Mbps */
#define	WI_APRATE_5		0x37		/* 5.5 Mbps */
#define	WI_APRATE_11		0x6E		/* 11 Mbps */
};

/*
 * Scan Results of Lucent chip
 */
struct wi_scan_data {
	u_int16_t		wi_chid;	/* BSS Channel ID from PR */
	u_int16_t		wi_noise;	/* Average Noise Level of the PR */
	u_int16_t		wi_signal;	/* Signal Level on the PR */
	u_int8_t		wi_bssid[6];	/* MACaddress of BSS responder from PR */
	u_int16_t		wi_interval;	/* BSS beacon interval */
	u_int16_t		wi_capinfo;	/* BSS Capability Information
						   IEEE Std 802.11(1997) ,see 7.3.1.4 */
	u_int16_t		wi_namelen;	/* Length of SSID strings */
	u_int8_t		wi_name[32];	/* SSID strings */
};

/*
 * transmit/receive frame structure
 */
struct wi_frame {
	u_int16_t		wi_status;	/* 0x00 */
	u_int16_t		wi_rx_tstamp1;	/* 0x02 */
	u_int16_t		wi_rx_tstamp0;	/* 0x04 */
	u_int8_t		wi_rx_silence;	/* 0x06 */
	u_int8_t		wi_rx_signal;	/* 0x07 */
	u_int8_t		wi_rx_rate;	/* 0x08 */
	u_int8_t		wi_rx_flow;	/* 0x09 */
	u_int8_t		wi_tx_rtry;	/* 0x0a */ /* Prism2 AP Only */
	u_int8_t		wi_tx_rate;	/* 0x0b */ /* Prism2 AP Only */
	u_int16_t		wi_tx_ctl;	/* 0x0c */
	struct ieee80211_frame_addr4 wi_whdr;	/* 0x0e */
	u_int16_t		wi_dat_len;	/* 0x2c */
	struct ether_header	wi_ehdr;	/* 0x2e */
} __attribute__((__packed__));

/* Tx Status Field */
#define	WI_TXSTAT_RET_ERR	0x0001
#define	WI_TXSTAT_AGED_ERR	0x0002
#define	WI_TXSTAT_DISCONNECT	0x0004
#define	WI_TXSTAT_FORM_ERR	0x0008

/* Rx Status Field */
#define WI_STAT_BADCRC		0x0001
#define WI_STAT_UNDECRYPTABLE	0x0002
#define WI_STAT_ERRSTAT		0x0003
#define WI_STAT_MAC_PORT	0x0700
#define	WI_STAT_PCF		0x1000
#define WI_RXSTAT_MSG_TYPE	0xE000
#define  WI_STAT_1042		0x2000	/* RFC1042 encoded */
#define  WI_STAT_TUNNEL		0x4000	/* Bridge-tunnel encoded */
#define  WI_STAT_WMP_MSG	0x6000	/* WaveLAN-II management protocol */
#define	 WI_STAT_MGMT		0x8000	/* 802.11b management frames */

#define WI_ENC_TX_E_II		0x0E

#define WI_ENC_TX_1042		0x00
#define WI_ENC_TX_TUNNEL	0xF8

/* TxControl Field (enhanced) */
#define	WI_TXCNTL_TX_OK		0x0002
#define	WI_TXCNTL_TX_EX		0x0004
#define	WI_TXCNTL_STRUCT_TYPE	0x0018
#define	 WI_ENC_TX_802_3	0x00
#define	 WI_ENC_TX_802_11	0x08
#define	WI_TXCNTL_ALTRTRY	0x0020
#define	WI_TXCNTL_NOCRYPT	0x0080

/*
 * HFA3861/3863 (BBP) Control Registers
 */
#define WI_HFA386X_CR_A_D_TEST_MODES2 0x1A
#define WI_HFA386X_CR_MANUAL_TX_POWER 0x3E

#ifdef IEEE80211_RADIOTAP_F_CFP
/*
 * Radio capture format for Prism.
 */
#define WI_RX_RADIOTAP_PRESENT \
	((1 << IEEE80211_RADIOTAP_TSFT) | \
	 (1 << IEEE80211_RADIOTAP_FLAGS) | \
	 (1 << IEEE80211_RADIOTAP_RATE) | \
	 (1 << IEEE80211_RADIOTAP_CHANNEL) | \
	 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) | \
	 (1 << IEEE80211_RADIOTAP_DB_ANTNOISE))

struct wi_rx_radiotap_header {
	struct ieee80211_radiotap_header wr_ihdr;
	u_int64_t	wr_tsf;
	u_int8_t	wr_flags;
	u_int8_t	wr_rate;
	u_int16_t	wr_chan_freq;
	u_int16_t	wr_chan_flags;
	u_int8_t	wr_antsignal;
	u_int8_t	wr_antnoise;
};

#define WI_TX_RADIOTAP_PRESENT \
	((1 << IEEE80211_RADIOTAP_FLAGS) | \
	 (1 << IEEE80211_RADIOTAP_RATE) | \
	 (1 << IEEE80211_RADIOTAP_CHANNEL))

struct wi_tx_radiotap_header {
	struct ieee80211_radiotap_header wt_ihdr;
	u_int8_t	wt_flags;
	u_int8_t	wt_rate;
	u_int16_t	wt_chan_freq;
	u_int16_t	wt_chan_flags;
};
#endif /* IEEE80211_RADIOTAP_F_CFP */


code:
/*-
 * Copyright (c) 2002
 *	M Warner Losh <imp@freebsd.org>.  All rights reserved.
 * Copyright (c) 1997, 1998, 1999
 *	Bill Paul <wpaul@ctr.columbia.edu>.  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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``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 Bill Paul OR THE VOICES IN HIS HEAD
 * 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.
 *
 * $FreeBSD: release/9.0.0/sys/dev/wi/if_wivar.h 194023 2009-06-11 17:14:28Z avg $
 */

/*
 * Encryption controls. We can enable or disable encryption as
 * well as specify up to 4 encryption keys. We can also specify
 * which of the four keys will be used for transmit encryption.
 */
#define WI_RID_ENCRYPTION	0xFC20
#define WI_RID_AUTHTYPE		0xFC21
#define WI_RID_DEFLT_CRYPT_KEYS	0xFCB0
#define WI_RID_TX_CRYPT_KEY	0xFCB1
#define WI_RID_WEP_AVAIL	0xFD4F
#define WI_RID_P2_TX_CRYPT_KEY	0xFC23
#define WI_RID_P2_CRYPT_KEY0	0xFC24
#define WI_RID_P2_CRYPT_KEY1	0xFC25
#define WI_RID_MICROWAVE_OVEN	0xFC25
#define WI_RID_P2_CRYPT_KEY2	0xFC26
#define WI_RID_P2_CRYPT_KEY3	0xFC27
#define WI_RID_P2_ENCRYPTION	0xFC28
#define WI_RID_ROAMING_MODE	0xFC2D
#define WI_RID_CUR_TX_RATE	0xFD44 /* current TX rate */

#define	WI_MAX_AID		256	/* max stations for ap operation */

struct wi_vap {
	struct ieee80211vap	wv_vap;
	struct ieee80211_beacon_offsets	wv_bo;

	void		(*wv_recv_mgmt)(struct ieee80211_node *, struct mbuf *,
			    int, int, int);
	int		(*wv_newstate)(struct ieee80211vap *,
			    enum ieee80211_state, int);
};
#define	WI_VAP(vap)		((struct wi_vap *)(vap))

struct wi_softc	{
	struct ifnet		*sc_ifp;
	device_t		sc_dev;
	struct mtx		sc_mtx;
	struct callout		sc_watchdog;
	int			sc_unit;
	int			wi_gone;
	int			sc_enabled;
	int			sc_reset;
	int			sc_firmware_type;
#define WI_NOTYPE	0
#define	WI_LUCENT	1
#define	WI_INTERSIL	2
#define	WI_SYMBOL	3
	int			sc_pri_firmware_ver;	/* Primary firmware */
	int			sc_sta_firmware_ver;	/* Station firmware */
	unsigned int		sc_nic_id;		/* Type of NIC */
	char *			sc_nic_name;

	int			wi_bus_type;	/* Bus attachment type */
	struct resource *	local;
	int			local_rid;
	struct resource *	iobase;
	int			iobase_rid;
	struct resource *	irq;
	int			irq_rid;
	struct resource *	mem;
	int			mem_rid;
	bus_space_handle_t	wi_localhandle;
	bus_space_tag_t		wi_localtag;
	bus_space_handle_t	wi_bhandle;
	bus_space_tag_t		wi_btag;
	bus_space_handle_t	wi_bmemhandle;
	bus_space_tag_t		wi_bmemtag;
	void *			wi_intrhand;
	struct ieee80211_channel *wi_channel;
	int			wi_io_addr;
	int			wi_cmd_count;

	int			sc_flags;
	int			sc_if_flags;
	int			sc_bap_id;
	int			sc_bap_off;

	int			sc_porttype;
	u_int16_t		sc_portnum;
	u_int16_t		sc_encryption;
	u_int16_t		sc_monitor_port;

	/* RSSI interpretation */
	u_int16_t		sc_min_rssi;	/* clamp sc_min_rssi < RSSI */
	u_int16_t		sc_max_rssi;	/* clamp RSSI < sc_max_rssi */
	u_int16_t		sc_dbm_offset;	/* dBm ~ RSSI - sc_dbm_offset */

	int			sc_buflen;		/* TX buffer size */
	int			sc_ntxbuf;
#define	WI_NTXBUF	3
	struct {
		int		d_fid;
		int		d_len;
	}			sc_txd[WI_NTXBUF];	/* TX buffers */
	int			sc_txnext;		/* index of next TX */
	int			sc_txcur;		/* index of current TX*/
	int			sc_tx_timer;

	struct wi_counters	sc_stats;
	u_int16_t		sc_ibss_port;

	struct timeval		sc_last_syn;
	int			sc_false_syns;

	u_int16_t		sc_txbuf[IEEE80211_MAX_LEN/2];

	struct wi_tx_radiotap_header sc_tx_th;
	struct wi_rx_radiotap_header sc_rx_th;
};

/* maximum consecutive false change-of-BSSID indications */
#define	WI_MAX_FALSE_SYNS		10

#define	WI_FLAGS_HAS_ENHSECURITY	0x0001
#define	WI_FLAGS_HAS_WPASUPPORT		0x0002
#define	WI_FLAGS_HAS_ROAMING		0x0020
#define	WI_FLAGS_HAS_FRAGTHR		0x0200
#define	WI_FLAGS_HAS_DBMADJUST		0x0400

struct wi_card_ident {
	u_int16_t	card_id;
	char		*card_name;
	u_int8_t	firm_type;
};

#define	WI_PRISM_MIN_RSSI	0x1b
#define	WI_PRISM_MAX_RSSI	0x9a
#define	WI_PRISM_DBM_OFFSET	100 /* XXX */

#define	WI_LUCENT_MIN_RSSI	47
#define	WI_LUCENT_MAX_RSSI	138
#define	WI_LUCENT_DBM_OFFSET	149

#define	WI_RSSI_TO_DBM(sc, rssi) (MIN((sc)->sc_max_rssi, \
    MAX((sc)->sc_min_rssi, (rssi))) - (sc)->sc_dbm_offset)

#define	WI_LOCK(_sc) 		mtx_lock(&(_sc)->sc_mtx)
#define	WI_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
#define	WI_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)

int	wi_attach(device_t);
int	wi_detach(device_t);
int	wi_shutdown(device_t);
int	wi_alloc(device_t, int);
void	wi_free(device_t);
extern devclass_t wi_devclass;
void	wi_init(void *);
void	wi_intr(void *);
int	wi_mgmt_xmit(struct wi_softc *, caddr_t, int);
void	wi_stop(struct wi_softc *, int);

repo_name: veiledmerc/mira

license: mit


code:
/*-
 * Copyright (c) 2000 Michael Smith
 * Copyright (c) 2000 BSDi
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 *
 *	$FreeBSD: release/9.0.0/lib/libdevinfo/devinfo.h 218505 2011-02-10 04:53:09Z imp $
 */

#ifndef _DEVINFO_H_INCLUDED
#define _DEVINFO_H_INCLUDED

#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/bus.h>

typedef __uintptr_t	devinfo_handle_t;
#define DEVINFO_ROOT_DEVICE	((devinfo_handle_t)0)

typedef enum device_state devinfo_state_t;

struct devinfo_dev {
	devinfo_handle_t	dd_handle;	/* device handle */
	devinfo_handle_t	dd_parent;	/* parent handle */

	char			*dd_name;	/* name of device */
	char			*dd_desc;	/* device description */
	char			*dd_drivername;	/* name of attached driver*/
	char			*dd_pnpinfo;	/* pnp info from parent bus */
	char			*dd_location;	/* Where bus thinks dev at */
	uint32_t		dd_devflags;	/* API flags */
	uint16_t		dd_flags;	/* internal dev flags */
	devinfo_state_t		dd_state;	/* attacement state of dev */
};

struct devinfo_rman {
	devinfo_handle_t	dm_handle;	/* resource manager handle */

	unsigned long		dm_start;	/* resource start */
	unsigned long		dm_size;	/* resource size */

	char			*dm_desc;	/* resource description */
};

struct devinfo_res {
	devinfo_handle_t	dr_handle;	/* resource handle */
	devinfo_handle_t	dr_rman;	/* resource manager handle */
	devinfo_handle_t	dr_device;	/* owning device */

	unsigned long		dr_start;	/* region start */
	unsigned long		dr_size;	/* region size */
	/* XXX add flags */
};

__BEGIN_DECLS

/*
 * Acquire a coherent copy of the kernel's device and resource tables.
 * This must return success (zero) before any other interfaces will
 * function.  Sets errno on failure.
 */
extern int	devinfo_init(void);

/*
 * Release the storage associated with the internal copy of the device
 * and resource tables. devinfo_init must be called before any attempt
 * is made to use any other interfaces.
 */
extern void	devinfo_free(void);

/*
 * Find a device/resource/resource manager by its handle.
 */
extern struct devinfo_dev
	*devinfo_handle_to_device(devinfo_handle_t handle);
extern struct devinfo_res
	*devinfo_handle_to_resource(devinfo_handle_t handle);
extern struct devinfo_rman
	*devinfo_handle_to_rman(devinfo_handle_t handle);

/*
 * Iterate over the children of a device, calling (fn) on each.  If
 * (fn) returns nonzero, abort the scan and return.
 */
extern int
	devinfo_foreach_device_child(struct devinfo_dev *parent,
	    int (* fn)(struct devinfo_dev *child, void *arg),
	    void *arg);

/*
 * Iterate over all the resources owned by a device, calling (fn) on each.
 * If (fn) returns nonzero, abort the scan and return.
 */
extern int
	devinfo_foreach_device_resource(struct devinfo_dev *dev,
	    int (* fn)(struct devinfo_dev *dev,
	    struct devinfo_res *res, void *arg),
	    void *arg);

/*
 * Iterate over all the resources owned by a resource manager, calling (fn)
 * on each.  If (fn) returns nonzero, abort the scan and return.
 */
extern int
	devinfo_foreach_rman_resource(struct devinfo_rman *rman,
	    int (* fn)(struct devinfo_res *res, void *arg),
	    void *arg);

/*
 * Iterate over all the resource managers, calling (fn) on each.  If (fn)
 * returns nonzero, abort the scan and return.
 */
extern int
	devinfo_foreach_rman(int (* fn)(struct devinfo_rman *rman, void *arg),
	    void *arg);

__END_DECLS

#endif /* ! _DEVINFO_H_INCLUDED */


code:
/*
 * Copyright (c) 1997, 1998 Kenneth D. Merry.
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 *
 * $FreeBSD: release/9.0.0/lib/libdevstat/devstat.h 175861 2008-01-31 16:55:12Z jhb $
 */

#ifndef _DEVSTAT_H
#define _DEVSTAT_H
#include <sys/cdefs.h>
#include <sys/devicestat.h>
#include <sys/resource.h>

#include <kvm.h>

/*
 * Bumped every time we change the userland API.  Hopefully this doesn't
 * happen very often!  This should be bumped every time we have to
 * increment SHLIB_MAJOR in the libdevstat Makefile (for non-backwards
 * compatible API changes) and should also be bumped every time we make
 * backwards-compatible API changes, so application writers have a way to
 * determine when a particular feature is available.
 */
#define	DEVSTAT_USER_API_VER	6

#define DEVSTAT_ERRBUF_SIZE  2048 /* size of the devstat library error string */

extern char devstat_errbuf[];

typedef enum {
	DEVSTAT_MATCH_NONE	= 0x00,
	DEVSTAT_MATCH_TYPE	= 0x01,
	DEVSTAT_MATCH_IF	= 0x02,
	DEVSTAT_MATCH_PASS	= 0x04
} devstat_match_flags;

typedef enum {
	DSM_NONE,
	DSM_TOTAL_BYTES,
	DSM_TOTAL_BYTES_READ,
	DSM_TOTAL_BYTES_WRITE,
	DSM_TOTAL_TRANSFERS,
	DSM_TOTAL_TRANSFERS_READ,
	DSM_TOTAL_TRANSFERS_WRITE,
	DSM_TOTAL_TRANSFERS_OTHER,
	DSM_TOTAL_BLOCKS,
	DSM_TOTAL_BLOCKS_READ,
	DSM_TOTAL_BLOCKS_WRITE,
	DSM_KB_PER_TRANSFER,
	DSM_KB_PER_TRANSFER_READ,
	DSM_KB_PER_TRANSFER_WRITE,
	DSM_TRANSFERS_PER_SECOND,
	DSM_TRANSFERS_PER_SECOND_READ,
	DSM_TRANSFERS_PER_SECOND_WRITE,
	DSM_TRANSFERS_PER_SECOND_OTHER,
	DSM_MB_PER_SECOND,
	DSM_MB_PER_SECOND_READ,
	DSM_MB_PER_SECOND_WRITE,
	DSM_BLOCKS_PER_SECOND,
	DSM_BLOCKS_PER_SECOND_READ,
	DSM_BLOCKS_PER_SECOND_WRITE,
	DSM_MS_PER_TRANSACTION,
	DSM_MS_PER_TRANSACTION_READ,
	DSM_MS_PER_TRANSACTION_WRITE,
	DSM_SKIP,
	DSM_TOTAL_BYTES_FREE,
	DSM_TOTAL_TRANSFERS_FREE,
	DSM_TOTAL_BLOCKS_FREE,
	DSM_KB_PER_TRANSFER_FREE,
	DSM_MB_PER_SECOND_FREE,
	DSM_TRANSFERS_PER_SECOND_FREE,
	DSM_BLOCKS_PER_SECOND_FREE,
	DSM_MS_PER_TRANSACTION_OTHER,
	DSM_MS_PER_TRANSACTION_FREE,
	DSM_BUSY_PCT,
	DSM_QUEUE_LENGTH,
	DSM_MAX
} devstat_metric;

struct devstat_match {
	devstat_match_flags	match_fields;
	devstat_type_flags	device_type;
	int			num_match_categories;
};

struct devstat_match_table {
	const char *		match_str;
	devstat_type_flags	type;
	devstat_match_flags	match_field;
};

struct device_selection {
	u_int32_t	device_number;
	char		device_name[DEVSTAT_NAME_LEN];
	int		unit_number;
	int		selected;
	u_int64_t	bytes;
	int		position;
};

struct devinfo {
	struct devstat	*devices;
	u_int8_t	*mem_ptr;
	long		generation;
	int		numdevs;
};

struct statinfo {
	long		cp_time[CPUSTATES];
	long		tk_nin;
	long		tk_nout;
	struct devinfo	*dinfo;
	long double 	snap_time;
};

typedef enum {
	DS_SELECT_ADD,
	DS_SELECT_ONLY,
	DS_SELECT_REMOVE,
	DS_SELECT_ADDONLY
} devstat_select_mode;

__BEGIN_DECLS

int devstat_getnumdevs(kvm_t *kd);
long devstat_getgeneration(kvm_t *kd);
int devstat_getversion(kvm_t *kd);
int devstat_checkversion(kvm_t *kd);
int devstat_getdevs(kvm_t *kd, struct statinfo *stats);
int devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
		       int *num_selections, long *select_generation,
		       long current_generation, struct devstat *devices,
		       int numdevs, struct devstat_match *matches,
		       int num_matches, char **dev_selections,
		       int num_dev_selections, devstat_select_mode select_mode,
		       int maxshowdevs, int perf_select);
int devstat_buildmatch(char *match_str, struct devstat_match **matches,
		       int *num_matches);
int devstat_compute_statistics(struct devstat *current,
			       struct devstat *previous,
			       long double etime, ...);
long double devstat_compute_etime(struct bintime *cur_time,
				  struct bintime *prev_time);
__END_DECLS

#endif /* _DEVSTAT_H  */


code:
/*
 *  $Id: dialog.h,v 1.231 2011/06/29 09:51:00 tom Exp $
 *
 *  dialog.h -- common declarations for all dialog modules
 *
 *  Copyright 2000-2010,2011	Thomas E. Dickey
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License, version 2.1
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to
 *	Free Software Foundation, Inc.
 *	51 Franklin St., Fifth Floor
 *	Boston, MA 02110, USA.
 *
 *  An earlier version of this program lists as authors
 *	Savio Lam (lam836@cs.cuhk.hk)
 */

#ifndef DIALOG_H_included
#define DIALOG_H_included 1
/* *INDENT-OFF* */

#include <dlg_config.h>

#ifdef __hpux
#define __HP_CURSES_COMPAT	/* workaround for getattrs, etc. */
#endif

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>	/* fork() etc. */
#include <math.h>	/* sqrt() */

/* header conflict with Solaris xpg4 versus <sys/regset.h> */
#if defined(ERR) && (ERR == 13)
#undef ERR
#endif

#if defined(HAVE_NCURSESW_NCURSES_H)
#include <ncursesw/ncurses.h>
#elif defined(HAVE_NCURSES_NCURSES_H)
#include <ncurses/ncurses.h>
#elif defined(HAVE_NCURSES_CURSES_H)
#include <ncurses/curses.h>
#elif defined(HAVE_NCURSES_H)
#include <ncurses.h>
#else
#include <curses.h>
#endif

/* most curses.h headers include this, some do not */
#if defined(HAVE_UNCTRL_H)
#include <unctrl.h>
#endif

/* Solaris xpg4 renames these */
#ifndef KEY_MAX
#ifdef __KEY_MAX
#define KEY_MAX __KEY_MAX
#endif
#endif

#ifndef KEY_MIN
#ifdef __KEY_MIN
#define KEY_MIN __KEY_MIN
#endif
#endif

/* possible conflicts with <term.h> which may be included in <curses.h> */
#ifdef color_names
#undef color_names
#endif

#ifdef buttons
#undef buttons
#endif

#ifdef ENABLE_NLS
#include <libintl.h>
#include <langinfo.h>
#define _(s) dgettext(PACKAGE, s)
#else
#undef _
#define _(s) s
#endif

#ifndef GCC_NORETURN
#define GCC_NORETURN /*nothing*/
#endif

#ifndef GCC_UNUSED
#define GCC_UNUSED /*nothing*/
#endif

#ifndef HAVE_WGET_WCH
#undef USE_WIDE_CURSES
#endif

/*
 * FIXME: a configure check would be useful
 */
#ifdef __hpux
#undef ACS_UARROW
#undef ACS_DARROW
#undef ACS_BLOCK
#endif

/*
 * Change these if you want
 */
#define USE_SHADOW TRUE
#define USE_COLORS TRUE

#ifdef HAVE_COLOR
#define SCOLS	(COLS - (dialog_state.use_shadow ? 2 : 0))
#define SLINES	(LINES - (dialog_state.use_shadow ? 1 : 0))
#else
#define SCOLS	COLS
#define SLINES	LINES
#endif

#define DLG_EXIT_ESC		255
#define DLG_EXIT_UNKNOWN	-2	/* never return this (internal use) */
#define DLG_EXIT_ERROR		-1	/* the shell sees this as 255 */
#define DLG_EXIT_OK		0
#define DLG_EXIT_CANCEL		1
#define DLG_EXIT_HELP		2
#define DLG_EXIT_EXTRA		3
#define DLG_EXIT_ITEM_HELP	4	/* actually DLG_EXIT_HELP */

#define DLG_CTRL(n)	((n) & 0x1f)	/* CTRL is preferred, but conflicts */

#define CHR_HELP	DLG_CTRL('E')
#define CHR_BACKSPACE	DLG_CTRL('H')
#define CHR_REPAINT	DLG_CTRL('L')
#define CHR_KILL	DLG_CTRL('U')
#define CHR_LITERAL	DLG_CTRL('V')
#define CHR_DELETE	127
#define CHR_NEXT	DLG_CTRL('N')
#define CHR_PREVIOUS	DLG_CTRL('P')
#define CHR_TRACE	DLG_CTRL('T')

#define ESC		27
#define TAB		DLG_CTRL('I')

#define MARGIN 1
#define GUTTER 2
#define SHADOW_ROWS 1
#define SHADOW_COLS 2
#define ARROWS_COL  5

#define MAX_LEN 2048
#define BUF_SIZE (10L*1024)

#undef  MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y))

#undef  MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))

#define DEFAULT_SEPARATE_STR "\t"
#define DEFAULT_ASPECT_RATIO 9
/* how many spaces is a tab long (default)? */
#define TAB_LEN 8
#define WTIMEOUT_VAL        10	/* minimum amount of time needed for curses */

#ifndef A_CHARTEXT
#define A_CHARTEXT 0xff
#endif

#define CharOf(ch)  ((ch) & 0xff)

#ifndef ACS_ULCORNER
#define ACS_ULCORNER '+'
#endif
#ifndef ACS_LLCORNER
#define ACS_LLCORNER '+'
#endif
#ifndef ACS_URCORNER
#define ACS_URCORNER '+'
#endif
#ifndef ACS_LRCORNER
#define ACS_LRCORNER '+'
#endif
#ifndef ACS_HLINE
#define ACS_HLINE '-'
#endif
#ifndef ACS_VLINE
#define ACS_VLINE '|'
#endif
#ifndef ACS_LTEE
#define ACS_LTEE '+'
#endif
#ifndef ACS_RTEE
#define ACS_RTEE '+'
#endif
#ifndef ACS_UARROW
#define ACS_UARROW '^'
#endif
#ifndef ACS_DARROW
#define ACS_DARROW 'v'
#endif
#ifndef ACS_BLOCK
#define ACS_BLOCK '#'
#endif

/* these definitions may work for antique versions of curses */
#ifndef HAVE_GETBEGYX
#undef  getbegyx
#define getbegyx(win,y,x)	(y = (win)?(win)->_begy:ERR, x = (win)?(win)->_begx:ERR)
#endif

#ifndef HAVE_GETMAXYX
#undef  getmaxyx
#define getmaxyx(win,y,x)	(y = (win)?(win)->_maxy:ERR, x = (win)?(win)->_maxx:ERR)
#endif

#ifndef HAVE_GETPARYX
#undef  getparyx
#define getparyx(win,y,x)	(y = (win)?(win)->_pary:ERR, x = (win)?(win)->_parx:ERR)
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* these definitions may be needed for bleeding-edge curses implementations */
#if !(defined(HAVE_GETBEGX) && defined(HAVE_GETBEGY))
#undef getbegx
#undef getbegy
#define getbegx(win) dlg_getbegx(win)
#define getbegy(win) dlg_getbegy(win)
extern int dlg_getbegx(WINDOW * /*win*/);
extern int dlg_getbegy(WINDOW * /*win*/);
#endif

#if !(defined(HAVE_GETCURX) && defined(HAVE_GETCURY))
#undef getcurx
#undef getcury
#define getcurx(win) dlg_getcurx(win)
#define getcury(win) dlg_getcury(win)
extern int dlg_getcurx(WINDOW * /*win*/);
extern int dlg_getcury(WINDOW * /*win*/);
#endif

#if !(defined(HAVE_GETMAXX) && defined(HAVE_GETMAXY))
#undef getmaxx
#undef getmaxy
#define getmaxx(win) dlg_getmaxx(win)
#define getmaxy(win) dlg_getmaxy(win)
extern int dlg_getmaxx(WINDOW * /*win*/);
extern int dlg_getmaxy(WINDOW * /*win*/);
#endif

#if !(defined(HAVE_GETPARX) && defined(HAVE_GETPARY))
#undef getparx
#undef getpary
#define getparx(win) dlg_getparx(win)
#define getpary(win) dlg_getpary(win)
extern int dlg_getparx(WINDOW * /*win*/);
extern int dlg_getpary(WINDOW * /*win*/);
#endif

/*
 * This is a list of "old" names, which should be helpful in updating
 * applications that use libdialog.  Starting with 2003/11/26, all exported
 * symbols from libdialog have "dlg_" prefix, or "dialog_" prefix or "_dialog"
 * suffix (or suffix "_dialog", e.g., init_dialog).
 */
#ifdef __DIALOG_OLD_NAMES__
#define color_table                       dlg_color_table
#define attr_clear(win,h,w,a)             dlg_attr_clear(win,h,w,a)
#define auto_size(t,s,h,w,xl,mc)          dlg_auto_size(t,s,h,w,xl,mc)
#define auto_sizefile(t,f,h,w,xl,mc)      dlg_auto_sizefile(t,f,h,w,xl,mc)
#define beeping()                         dlg_beeping()
#define box_x_ordinate(w)                 dlg_box_x_ordinate(w)
#define box_y_ordinate(h)                 dlg_box_y_ordinate(h)
#define calc_listh(h,lh,in)               dlg_calc_listh(h,lh,in)
#define calc_listw(in,items,group)        dlg_calc_listw(in,items,group)
#define color_setup()                     dlg_color_setup()
#define create_rc(f)                      dlg_create_rc(f)
#define ctl_size(h,w)                     dlg_ctl_size(h,w)
#define del_window(win)                   dlg_del_window(win)
#define dialog_clear()                    dlg_clear()
#define draw_bottom_box(win)              dlg_draw_bottom_box(win)
#define draw_box(win,y,x,h,w,xc,bc)       dlg_draw_box(win,y,x,h,w,xc,bc)
#define draw_shadow(win,h,w,y,x)          dlg_draw_shadow(win,h,w,y,x)
#define draw_title(win,t)                 dlg_draw_title(win,t)
#define exiterr                           dlg_exiterr
#define killall_bg(n)                     dlg_killall_bg(n)
#define mouse_bigregion(y,x)              dlg_mouse_bigregion(y,x)
#define mouse_free_regions()              dlg_mouse_free_regions()
#define mouse_mkbigregion(y,x,h,w,n,ix,iy,m) dlg_mouse_mkbigregion(y,x,h,w,n,ix,iy,m)
#define mouse_mkregion(y,x,h,w,n)         dlg_mouse_mkregion(y,x,h,w,n)
#define mouse_region(y,x)                 dlg_mouse_region(y,x)
#define mouse_setbase(x,y)                dlg_mouse_setbase(x,y)
#define mouse_wgetch(w,c)                 dlg_mouse_wgetch(w,c)
#define new_window(h,w,y,x)               dlg_new_window(h,w,y,x)
#define parse_rc()                        dlg_parse_rc()
#define print_autowrap(win,s,h,w)         dlg_print_autowrap(win,s,h,w)
#define print_size(h,w)                   dlg_print_size(h,w)
#define put_backtitle()                   dlg_put_backtitle()
#define strclone(cprompt)                 dlg_strclone(cprompt)
#define sub_window(win,h,w,y,x)           dlg_sub_window(win,h,w,y,x)
#define tab_correct_str(s)                dlg_tab_correct_str(s)
#endif

/*
 * Attribute names
 */
#define DIALOG_ATR(n)                 dlg_color_table[n].atr

#define screen_attr                   DIALOG_ATR(0)
#define shadow_attr                   DIALOG_ATR(1)
#define dialog_attr                   DIALOG_ATR(2)
#define title_attr                    DIALOG_ATR(3)
#define border_attr                   DIALOG_ATR(4)
#define button_active_attr            DIALOG_ATR(5)
#define button_inactive_attr          DIALOG_ATR(6)
#define button_key_active_attr        DIALOG_ATR(7)
#define button_key_inactive_attr      DIALOG_ATR(8)
#define button_label_active_attr      DIALOG_ATR(9)
#define button_label_inactive_attr    DIALOG_ATR(10)
#define inputbox_attr                 DIALOG_ATR(11)
#define inputbox_border_attr          DIALOG_ATR(12)
#define searchbox_attr                DIALOG_ATR(13)
#define searchbox_title_attr          DIALOG_ATR(14)
#define searchbox_border_attr         DIALOG_ATR(15)
#define position_indicator_attr       DIALOG_ATR(16)
#define menubox_attr                  DIALOG_ATR(17)
#define menubox_border_attr           DIALOG_ATR(18)
#define item_attr                     DIALOG_ATR(19)
#define item_selected_attr            DIALOG_ATR(20)
#define tag_attr                      DIALOG_ATR(21)
#define tag_selected_attr             DIALOG_ATR(22)
#define tag_key_attr                  DIALOG_ATR(23)
#define tag_key_selected_attr         DIALOG_ATR(24)
#define check_attr                    DIALOG_ATR(25)
#define check_selected_attr           DIALOG_ATR(26)
#define uarrow_attr                   DIALOG_ATR(27)
#define darrow_attr                   DIALOG_ATR(28)
#define itemhelp_attr                 DIALOG_ATR(29)
#define form_active_text_attr         DIALOG_ATR(30)
#define form_text_attr                DIALOG_ATR(31)
#define form_item_readonly_attr       DIALOG_ATR(32)
#define gauge_attr                    DIALOG_ATR(33)

#define DLGK_max (KEY_MAX + 256)

/*
 * Callbacks are used to implement the "background" tailbox.
 */
struct _dlg_callback;

typedef void (*DIALOG_FREEBACK) (struct _dlg_callback * /* p */);

typedef struct _dlg_callback {
    struct _dlg_callback *next;
    FILE *input;
    WINDOW *win;
    bool keep_bg;	/* keep in background, on exit */
    bool bg_task;	/* true if this is background task */
    bool (*handle_getc)(struct _dlg_callback *p, int ch, int fkey, int *result);
    bool keep_win;	/* true to not erase window on exit */
    /* data for dlg_add_callback_ref */
    struct _dlg_callback **caller;
    DIALOG_FREEBACK freeback;
    /* 1.1-20110107 */
    bool (*handle_input)(struct _dlg_callback *p);
    bool input_ready;
} DIALOG_CALLBACK;

typedef struct _dlg_windows {
    struct _dlg_windows *next;
    WINDOW *normal;
    WINDOW *shadow;
} DIALOG_WINDOWS;

/*
 * Global variables, which are initialized as needed
 */
typedef struct {
    DIALOG_CALLBACK *getc_callbacks;
    DIALOG_CALLBACK *getc_redirect;
    DIALOG_WINDOWS *all_windows;
    FILE *output;		/* option "--output-fd fd" */
    FILE *pipe_input;		/* used for gauge widget */
    FILE *screen_output;	/* newterm(), etc. */
    bool screen_initialized;
    bool use_colors;		/* use colors by default? */
    bool use_scrollbar;		/* option "--scrollbar" */
    bool use_shadow;		/* shadow dialog boxes by default? */
    bool visit_items;		/* option "--visit-items" */
    char *separate_str;		/* option "--separate-widget string" */
    int aspect_ratio;		/* option "--aspect ratio" */
    int output_count;		/* # of widgets that may have done output */
    int tab_len;		/* option "--tab-len n" */
    /* 1.0-20070227 */
    FILE *input;		/* option "--input-fd fd" */
#ifdef HAVE_DLG_TRACE
    FILE *trace_output;		/* option "--trace file" */
#endif
    /* 1.1-20110106 */
    bool no_mouse;		/* option "--no-mouse" */
} DIALOG_STATE;

extern DIALOG_STATE dialog_state;

/*
 * Global variables, which dialog resets before each widget
 */
typedef struct {
    bool beep_after_signal;	/* option "--beep-after" */
    bool beep_signal;		/* option "--beep" */
    bool begin_set;		/* option "--begin y x" was used */
    bool cant_kill;		/* option "--no-kill" */
    bool colors;		/* option "--colors" */
    bool cr_wrap;		/* option "--cr-wrap" */
    bool defaultno;		/* option "--defaultno" */
    bool dlg_clear_screen;	/* option "--clear" */
    bool extra_button;		/* option "--extra-button" */
    bool help_button;		/* option "--help-button" */
    bool help_status;		/* option "--help-status" */
    bool input_menu;		/* menu vs inputmenu widget */
    bool insecure;		/* option "--insecure" */
    bool item_help;		/* option "--item-help" */
    bool keep_window;		/* option "--keep-window" */
    bool nocancel;		/* option "--no-cancel" */
    bool nocollapse;		/* option "--no-collapse" */
    bool print_siz;		/* option "--print-size" */
    bool separate_output;	/* option "--separate-output" */
    bool single_quoted;		/* option "--single-quoted" */
    bool size_err;		/* option "--size-err" */
    bool tab_correct;		/* option "--tab-correct" */
    bool trim_whitespace;	/* option "--trim" */
    char *backtitle;		/* option "--backtitle backtitle" */
    char *cancel_label;		/* option "--cancel-label string" */
    char *default_item;		/* option "--default-item string" */
    char *exit_label;		/* option "--exit-label string" */
    char *extra_label;		/* option "--extra-label string" */
    char *help_label;		/* option "--help-label string" */
    char *input_result;
    char *no_label;		/* option "--no-label string" */
    char *ok_label;		/* option "--ok-label string" */
    char *title;		/* option "--title title" */
    char *yes_label;		/* option "--yes-label string" */
    int begin_x;		/* option "--begin y x" (second value) */
    int begin_y;		/* option "--begin y x" (first value) */
    int max_input;		/* option "--max-input size" */
    int scale_factor;		/* RESERVED */
    int sleep_secs;		/* option "--sleep secs" */
    int timeout_secs;		/* option "--timeout secs" */
    unsigned input_length;	/* nonzero if input_result is allocated */
    /* 1.0-20051207 */
    unsigned formitem_type;	/* DIALOG_FORMITEM.type in dialog_form() */
    /* 1.1-20070227 */
    bool keep_tite;		/* option "--keep-tite" */
    bool ascii_lines;		/* option "--ascii-lines" */
    bool no_lines;		/* option "--no-lines" */
    /* 1.1-20070930 */
    bool nook;			/* option "--no-ok" */
    /* 1.1-20080727 */
    bool quoted;		/* option "--quoted" */
    char *column_header;	/* RESERVED "--column-header" */
    char *column_separator;	/* option "--column-separator" */
    char *output_separator;	/* option "--output-separator" */
    /* 1.1-20100118 */
    char *date_format;		/* option "--date-format" */
    char *time_format;		/* option "--time-format" */
    /* 1.1-20110629 */
    char *help_line;		/* option "--hline" */
    char *help_file;		/* option "--hfile" */
    bool in_helpfile;		/* flag to prevent recursion in --hfile */
    bool no_nl_expand;		/* option "--no-nl-expand" */
} DIALOG_VARS;

#define USE_ITEM_HELP(s)        (dialog_vars.item_help && (s) != 0)
#define CHECKBOX_TAGS           (dialog_vars.item_help ? 4 : 3)
#define MENUBOX_TAGS            (dialog_vars.item_help ? 3 : 2)
#define FORMBOX_TAGS            (dialog_vars.item_help ? 9 : 8)
#define MIXEDFORM_TAGS          (FORMBOX_TAGS + 1)
#define MIXEDGAUGE_TAGS         2

extern DIALOG_VARS dialog_vars;

#ifndef HAVE_TYPE_CHTYPE
#define chtype long
#endif

#define UCH(ch)			((unsigned char)(ch))

#define assert_ptr(ptr,msg) if ((ptr) == 0) dlg_exiterr("cannot allocate memory in " msg)

#define dlg_malloc(t,n)    (t *) malloc((size_t)(n) * sizeof(t))
#define dlg_calloc(t,n)    (t *) calloc((size_t)(n), sizeof(t))
#define dlg_realloc(t,n,p) (t *) realloc((p), (n) * sizeof(t))

/*
 * Table for attribute- and color-values.
 */
typedef struct {
    chtype atr;
#ifdef HAVE_COLOR
    int fg;
    int bg;
    int hilite;
#endif
#ifdef HAVE_RC_FILE
    const char *name;
    const char *comment;
#endif
} DIALOG_COLORS;

extern DIALOG_COLORS dlg_color_table[];

/*
 * Function prototypes
 */
extern const char *dialog_version(void);

/* widgets, each in separate files */
extern int dialog_calendar(const char * /*title*/, const char * /*subtitle*/, int /*height*/, int /*width*/, int /*day*/, int /*month*/, int /*year*/);
extern int dialog_checklist(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*list_height*/, int /*item_no*/, char ** /*items*/, int /*flag*/);
extern int dialog_dselect(const char * /*title*/, const char * /*path*/, int /*height*/, int /*width*/);
extern int dialog_editbox(const char * /*title*/, const char * /*file*/, int /*height*/, int /*width*/);
extern int dialog_form(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*form_height*/, int /*item_no*/, char ** /*items*/);
extern int dialog_fselect(const char * /*title*/, const char * /*path*/, int /*height*/, int /*width*/);
extern int dialog_gauge(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*percent*/);
extern int dialog_helpfile(const char * /*title*/, const char * /*file*/, int /*height*/, int /*width*/);
extern int dialog_inputbox(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, const char * /*init*/, const int /*password*/);
extern int dialog_menu(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*menu_height*/, int /*item_no*/, char ** /*items*/);
extern int dialog_mixedform(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*form_height*/, int /*item_no*/, char ** /*items*/);
extern int dialog_mixedgauge(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*percent*/, int /*item_no*/, char ** /*items*/);
extern int dialog_msgbox(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*pauseopt*/);
extern int dialog_pause(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*seconds*/);
extern int dialog_prgbox(const char * /*title*/, const char * /*cprompt*/, const char * /*command*/, int /*height*/, int /*width*/, int /*pauseopt*/);
extern int dialog_progressbox(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/);
extern int dialog_tailbox(const char * /*title*/, const char * /*file*/, int /*height*/, int /*width*/, int /*bg_task*/);
extern int dialog_textbox(const char * /*title*/, const char * /*file*/, int /*height*/, int /*width*/);
extern int dialog_timebox(const char * /*title*/, const char * /*subtitle*/, int /*height*/, int /*width*/, int /*hour*/, int /*minute*/, int /*second*/);
extern int dialog_yesno(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/);

/* some widgets have alternate entrypoints, to allow list manipulation */
typedef struct {
    char *name;
    char *text;
    char *help;
    int state;
} DIALOG_LISTITEM;

typedef struct {
    unsigned type;		/* the field type (0=input, 1=password) */
    char *name;			/* the field label */
    int name_len;		/* ...its length */
    int name_y;			/* ...its y-ordinate */
    int name_x;			/* ...its x-ordinate */
    bool name_free;		/* ...true if .name can be freed */
    char *text;			/* the field contents */
    int text_len;		/* ...its length on the screen */
    int text_y;			/* ...its y-ordinate */
    int text_x;			/* ...its x-ordinate */
    int text_flen;		/* ...its length on screen, or 0 if no input allowed */
    int text_ilen;		/* ...its limit on amount to be entered */
    bool text_free;		/* ...true if .text can be freed */
    char *help;			/* help-message, if any */
    bool help_free;		/* ...true if .help can be freed */
} DIALOG_FORMITEM;

typedef	int (DIALOG_INPUTMENU) (DIALOG_LISTITEM * /*items*/, int /*current*/, char * /*newtext*/);

extern int dlg_checklist(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*list_height*/, int /*item_no*/, DIALOG_LISTITEM * /*items*/, const char * /*states*/, int /*flag*/, int * /*current_item*/);
extern int dlg_form(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*form_height*/, int /*item_no*/, DIALOG_FORMITEM * /*items*/, int * /*current_item*/);
extern int dlg_menu(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*menu_height*/, int /*item_no*/, DIALOG_LISTITEM * /*items*/, int * /*current_item*/, DIALOG_INPUTMENU /*rename_menu*/);
extern int dlg_progressbox(const char * /*title*/, const char * /*cprompt*/, int /*height*/, int /*width*/, int /*pauseopt*/, FILE * /* fp */);

/* argv.c */
extern char ** dlg_string_to_argv(char * /* blob */);
extern int dlg_count_argv(char ** /* argv */);
extern int dlg_eat_argv(int * /* argcp */, char *** /* argvp */, int /* start */, int /* count */);

/* arrows.c */
extern void dlg_draw_arrows(WINDOW * /*dialog*/, int /*top_arrow*/, int /*bottom_arrow*/, int /*x*/, int /*top*/, int /*bottom*/);
extern void dlg_draw_arrows2(WINDOW * /*dialog*/, int /*top_arrow*/, int /*bottom_arrow*/, int /*x*/, int /*top*/, int /*bottom*/, chtype /*attr*/, chtype /*borderattr*/);
extern void dlg_draw_helpline(WINDOW * /*dialog*/, bool /*decorations*/);
extern void dlg_draw_scrollbar(WINDOW * /*dialog*/, long /* first_data */, long /* this_data */, long /* next_data */, long /* total_data */, int /* left */, int /* right */, int /*top*/, int /*bottom*/, chtype /*attr*/, chtype /*borderattr*/);

/* buttons.c */
extern const char ** dlg_exit_label(void);
extern const char ** dlg_ok_label(void);
extern const char ** dlg_ok_labels(void);
extern const char ** dlg_yes_labels(void);
extern int dlg_button_count(const char ** /*labels*/);
extern int dlg_button_to_char(const char * /*label*/);
extern int dlg_button_x_step(const char ** /*labels*/, int /*limit*/, int * /*gap*/, int * /*margin*/, int * /*step*/);
extern int dlg_char_to_button(int /*ch*/, const char ** /*labels*/);
extern int dlg_exit_buttoncode(int /*button*/);
extern int dlg_match_char(int /*ch*/, const char * /*string*/);
extern int dlg_next_button(const char ** /*labels*/, int /*button*/);
extern int dlg_next_ok_buttonindex(int /*current*/, int /*extra*/);
extern int dlg_ok_buttoncode(int /*button*/);
extern int dlg_prev_button(const char ** /*labels*/, int /*button*/);
extern int dlg_prev_ok_buttonindex(int /*current*/, int /*extra*/);
extern int dlg_yes_buttoncode(int /*button*/);
extern void dlg_button_layout(const char ** /*labels*/, int * /*limit*/);
extern void dlg_button_sizes(const char ** /*labels*/, int /*vertical*/, int * /*longest*/, int * /*length*/);
extern void dlg_draw_buttons(WINDOW * /*win*/, int /*y*/, int /*x*/, const char ** /*labels*/, int /*selected*/, int /*vertical*/, int /*limit*/);

/* columns.c */
extern void dlg_align_columns(char ** /* target */, int  /* per_row */, int /* num_rows */);
extern void dlg_free_columns(char ** /* target */, int  /* per_row */, int /* num_rows */);

/* editbox.c */
extern int dlg_editbox(const char */*title*/, char ***/*list*/, int */*rows*/, int /*height*/, int /*width*/);

/* formbox.c */
extern int dlg_default_formitem(DIALOG_FORMITEM * /*items*/);
extern int dlg_ordinate(const char * /*s*/);
extern void dlg_free_formitems(DIALOG_FORMITEM * /*items*/);

/* guage.c */
extern void * dlg_allocate_gauge(const char * /* title */, const char * /* cprompt */, int /* height */, int /* width */, int /* percent */);
extern void dlg_free_gauge(void * /* objptr */);
extern void dlg_update_gauge(void * /* objptr */, int /* percent */);

/* inputstr.c */
extern bool dlg_edit_string(char * /*string*/, int * /*offset*/, int /*key*/, int /*fkey*/, bool /*force*/);
extern const int * dlg_index_columns(const char * /*string*/);
extern const int * dlg_index_wchars(const char * /*string*/);
extern int dlg_count_columns(const char * /*string*/);
extern int dlg_count_wchars(const char * /*string*/);
extern int dlg_edit_offset(char * /*string*/, int /*offset*/, int /*x_last*/);
extern int dlg_find_index(const int * /*list*/, int  /*limit*/, int /*to_find*/);
extern int dlg_limit_columns(const char * /*string*/, int /*limit*/, int /*offset*/);
extern void dlg_show_string(WINDOW * /*win*/, const char * /*string*/, int /*offset*/, chtype /*attr*/, int /*y_base*/, int /*x_base*/, int /*x_last*/, bool /*hidden*/, bool /*force*/);

/* rc.c */
#ifdef HAVE_RC_FILE
extern int dlg_parse_rc(void);
extern void dlg_create_rc(const char * /*filename*/);
#endif

/* ui_getc.c */
extern int dlg_getc(WINDOW * /*win*/, int * /*fkey*/);
extern int dlg_getc_callbacks(int /*ch*/, int /*fkey*/, int * /*result*/);
extern int dlg_last_getc(void);
extern void dlg_add_callback(DIALOG_CALLBACK * /*p*/);
extern void dlg_add_callback_ref(DIALOG_CALLBACK ** /*p*/, DIALOG_FREEBACK /* cleanup */);
extern void dlg_flush_getc(void);
extern void dlg_remove_callback(DIALOG_CALLBACK * /*p*/);
extern void dlg_killall_bg(int *retval);

/* util.c */
extern WINDOW * dlg_new_modal_window(WINDOW * /*parent*/, int /*height*/, int /*width*/, int /*y*/, int /*x*/);
extern WINDOW * dlg_new_window(int /*height*/, int /*width*/, int /*y*/, int /*x*/);
extern WINDOW * dlg_sub_window(WINDOW * /*win*/, int /*height*/, int /*width*/, int /*y*/, int /*x*/);
extern bool dlg_need_separator(void);
extern char * dlg_set_result(const char * /*string*/);
extern char * dlg_strclone(const char * /*cprompt*/);
extern char * dlg_strempty(void);
extern chtype dlg_asciibox(chtype /*ch*/);
extern chtype dlg_boxchar(chtype /*ch*/);
extern chtype dlg_get_attrs(WINDOW * /*win*/);
extern const char * dlg_print_line(WINDOW */*win*/, chtype */*attr*/, const char */*prompt*/, int /*lm*/, int /*rm*/, int */*x*/);
extern int dlg_box_x_ordinate(int /*width*/);
extern int dlg_box_y_ordinate(int /*height*/);
extern int dlg_calc_list_width(int /*item_no*/, DIALOG_LISTITEM * /*items*/);
extern int dlg_calc_listw(int /*item_no*/, char ** /*items*/, int /*group*/);
extern int dlg_check_scrolled(int /* key */, int /* last */, int /* page */, bool */* show */, int */* offset */);
extern int dlg_default_item(char ** /*items*/, int /*llen*/);
extern int dlg_default_listitem(DIALOG_LISTITEM * /*items*/);
extern int dlg_defaultno_button(void);
extern int dlg_max_input(int /*max_len*/);
extern int dlg_print_scrolled(WINDOW * /* win */, const char * /* prompt */, int /* offset */, int /* height */, int /* width */, int /* pauseopt */);
extern void dlg_add_quoted(char * /*string*/);
extern void dlg_add_result(const char * /*string*/);
extern void dlg_add_separator(void);
extern void dlg_add_string(char * /*string*/);
extern void dlg_attr_clear(WINDOW * /*win*/, int /*height*/, int /*width*/, chtype /*attr*/);
extern void dlg_auto_size(const char * /*title*/, const char * /*prompt*/, int * /*height*/, int * /*width*/, int /*boxlines*/, int /*mincols*/);
extern void dlg_auto_sizefile(const char * /*title*/, const char * /*file*/, int * /*height*/, int * /*width*/, int /*boxlines*/, int /*mincols*/);
extern void dlg_beeping(void);
extern void dlg_calc_listh(int * /*height*/, int * /*list_height*/, int /*item_no*/);
extern void dlg_clear(void);
extern void dlg_clr_result(void);
extern void dlg_ctl_size(int /*height*/, int /*width*/);
extern void dlg_del_window(WINDOW * /*win*/);
extern void dlg_does_output(void);
extern void dlg_draw_bottom_box(WINDOW * /*win*/);
extern void dlg_draw_box(WINDOW * /*win*/, int /*y*/, int /*x*/, int /*height*/, int /*width*/, chtype /*boxchar*/, chtype /*borderchar*/);
extern void dlg_draw_title(WINDOW *win, const char *title);
extern void dlg_exit(int /*code*/) GCC_NORETURN;
extern void dlg_item_help(const char * /*txt*/);
extern void dlg_print_autowrap(WINDOW * /*win*/, const char * /*prompt*/, int /*height*/, int /*width*/);
extern void dlg_print_size(int /*height*/, int /*width*/);
extern void dlg_print_text(WINDOW * /*win*/, const char * /*txt*/, int /*len*/, chtype * /*attr*/);
extern void dlg_put_backtitle(void);
extern void dlg_restore_vars(DIALOG_VARS * /* save */);
extern void dlg_save_vars(DIALOG_VARS * /* save */);
extern void dlg_set_focus(WINDOW * /*parent*/, WINDOW * /*win*/);
extern void dlg_tab_correct_str(char * /*prompt*/);
extern void dlg_trim_string(char * /*src*/);
extern void end_dialog(void);
extern void init_dialog(FILE * /*input*/, FILE * /*output*/);

extern void dlg_exiterr(const char *, ...) GCC_NORETURN
#if defined(__GNUC__) && !defined(printf)
__attribute__((format(printf,1,2)))
#endif
;

#ifdef HAVE_COLOR
extern chtype dlg_color_pair(int /*foreground*/, int /*background*/);
extern int dlg_color_count(void);
extern void dlg_color_setup(void);
extern void dlg_draw_shadow(WINDOW * /*win*/, int /*height*/, int /*width*/, int /*y*/, int /*x*/);
#endif

#ifdef HAVE_STRCASECMP
#define dlg_strcmp(a,b) strcasecmp(a,b)
#else
extern int dlg_strcmp(const char * /*a*/, const char * /*b*/);
#endif

#ifdef HAVE_DLG_TRACE
#define DLG_TRACE(params) dlg_trace_msg params
extern void dlg_trace_msg(const char *fmt, ...)
#ifdef GCC_PRINTF
    __attribute__((format(printf,1,2)))
#endif
;
extern void dlg_trace_win(WINDOW * /*win*/);
extern void dlg_trace_chr(int /*ch*/, int /*fkey*/);
extern void dlg_trace(const char * /*fname*/);
#else
#define DLG_TRACE(params) /* nothing */
#define dlg_trace_win(win) /* nothing */
#define dlg_trace_chr(ch,fkey) /* nothing */
#define dlg_trace(fname) /* nothing */
#endif

#ifdef KEY_RESIZE
extern void dlg_move_window(WINDOW * /*win*/, int /*height*/, int /*width*/, int /*y*/, int /*x*/);
#endif

/*
 * Normally "enter" means "ok".  Use this macro to handle the explicit
 * check for DLGK_ENTER:
 */
#define dlg_enter_buttoncode(code) (dialog_vars.nook ? DLG_EXIT_OK : dlg_ok_buttoncode(code))

/*
 * The following stuff is needed for mouse support
 */
typedef struct mseRegion {
    int x, y, X, Y, code;
    int mode, step_x, step_y;
    struct mseRegion *next;
} mseRegion;

#if defined(NCURSES_MOUSE_VERSION)

#define	mouse_open() mousemask(BUTTON1_CLICKED, (mmask_t *) 0)
#define	mouse_close() mousemask(0, (mmask_t *) 0)

extern mseRegion * dlg_mouse_mkregion (int /*y*/, int /*x*/, int /*height*/, int /*width*/, int /*code*/);
extern void dlg_mouse_free_regions (void);
extern void dlg_mouse_mkbigregion (int /*y*/, int /*x*/, int /*height*/, int /*width*/, int /*code*/, int /*step_x*/, int /*step_y*/, int /*mode*/);
extern void dlg_mouse_setbase (int /*x*/, int /*y*/);

#define USE_MOUSE 1

#else

#define	mouse_open() /*nothing*/
#define	mouse_close() /*nothing*/
#define dlg_mouse_free_regions() /* nothing */
#define	dlg_mouse_mkregion(y, x, height, width, code) /*nothing*/
#define	dlg_mouse_mkbigregion(y, x, height, width, code, step_x, step_y, mode) /*nothing*/
#define	dlg_mouse_setbase(x, y) /*nothing*/

#define USE_MOUSE 0

#endif

extern mseRegion *dlg_mouse_region (int /*y*/, int /*x*/);
extern mseRegion *dlg_mouse_bigregion (int /*y*/, int /*x*/);
extern int dlg_mouse_wgetch (WINDOW * /*win*/, int * /*fkey*/);
extern int dlg_mouse_wgetch_nowait (WINDOW * /*win*/, int * /*fkey*/);

#define mouse_mkbutton(y,x,len,code) dlg_mouse_mkregion(y,x,1,len,code);

/*
 * This is the base for fictitious keys, which activate
 * the buttons.
 *
 * Mouse-generated keys are the following:
 *   -- the first 32 are used as numbers, in addition to '0'-'9'
 *   -- uppercase chars are used to invoke the button (M_EVENT + 'O')
 */
#define M_EVENT (DLGK_max + 1)

/*
 * The `flag' parameter in checklist is used to select between
 * radiolist and checklist
 */
#define FLAG_CHECK 1
#define FLAG_RADIO 0

/*
 * This is used only for debugging (FIXME: should have a separate header).
 */
#ifdef NO_LEAKS
extern void _dlg_inputstr_leaks(void);
#if defined(NCURSES_VERSION) && defined(HAVE__NC_FREE_AND_EXIT)
extern void _nc_free_and_exit(int);	/* nc_alloc.h normally not installed */
#endif
#endif

#ifdef __cplusplus
}
#endif
/* *INDENT-ON* */

#endif /* DIALOG_H_included */


code:
/* Generated from /usr/src/kerberos5/lib/libasn1/../../../crypto/heimdal/lib/asn1/digest.asn1 */
/* Do not edit */

#ifndef __digest_asn1_h__
#define __digest_asn1_h__

#include <stddef.h>
#include <time.h>

#ifndef __asn1_common_definitions__
#define __asn1_common_definitions__

typedef struct heim_integer {
  size_t length;
  void *data;
  int negative;
} heim_integer;

typedef struct heim_octet_string {
  size_t length;
  void *data;
} heim_octet_string;

typedef char *heim_general_string;

typedef char *heim_utf8_string;

typedef char *heim_printable_string;

typedef char *heim_ia5_string;

typedef struct heim_bmp_string {
  size_t length;
  uint16_t *data;
} heim_bmp_string;

typedef struct heim_universal_string {
  size_t length;
  uint32_t *data;
} heim_universal_string;

typedef char *heim_visible_string;

typedef struct heim_oid {
  size_t length;
  unsigned *components;
} heim_oid;

typedef struct heim_bit_string {
  size_t length;
  void *data;
} heim_bit_string;

typedef struct heim_octet_string heim_any;
typedef struct heim_octet_string heim_any_set;

#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R)                  \
  do {                                                         \
    (BL) = length_##T((S));                                    \
    (B) = malloc((BL));                                        \
    if((B) == NULL) {                                          \
      (R) = ENOMEM;                                            \
    } else {                                                   \
      (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \
                       (S), (L));                              \
      if((R) != 0) {                                           \
        free((B));                                             \
        (B) = NULL;                                            \
      }                                                        \
    }                                                          \
  } while (0)

struct units;

#endif

#include <krb5_asn1.h>
/*
DigestTypes ::= BIT STRING {
  ntlm-v1(0),
  ntlm-v1-session(1),
  ntlm-v2(2),
  digest-md5(3),
  chap-md5(4),
  ms-chap-v2(5)
}
*/

typedef struct DigestTypes {
  unsigned int ntlm_v1:1;
  unsigned int ntlm_v1_session:1;
  unsigned int ntlm_v2:1;
  unsigned int digest_md5:1;
  unsigned int chap_md5:1;
  unsigned int ms_chap_v2:1;
} DigestTypes;

int    encode_DigestTypes(unsigned char *, size_t, const DigestTypes *, size_t *);
int    decode_DigestTypes(const unsigned char *, size_t, DigestTypes *, size_t *);
void   free_DigestTypes  (DigestTypes *);
size_t length_DigestTypes(const DigestTypes *);
int    copy_DigestTypes  (const DigestTypes *, DigestTypes *);
unsigned DigestTypes2int(DigestTypes);
DigestTypes int2DigestTypes(unsigned);
#ifdef __PARSE_UNITS_H__
const struct units * asn1_DigestTypes_units(void);
#endif

/*
DigestInit ::= SEQUENCE {
  type              UTF8String,
  channel         [0] SEQUENCE {
    cb-type             UTF8String,
    cb-binding          UTF8String,
  } OPTIONAL,
  hostname        [1]   UTF8String OPTIONAL,
}
*/

typedef struct DigestInit {
  heim_utf8_string type;
  struct  {
    heim_utf8_string cb_type;
    heim_utf8_string cb_binding;
  } *channel;
  heim_utf8_string *hostname;
} DigestInit;

int    encode_DigestInit(unsigned char *, size_t, const DigestInit *, size_t *);
int    decode_DigestInit(const unsigned char *, size_t, DigestInit *, size_t *);
void   free_DigestInit  (DigestInit *);
size_t length_DigestInit(const DigestInit *);
int    copy_DigestInit  (const DigestInit *, DigestInit *);

/*
DigestInitReply ::= SEQUENCE {
  nonce             UTF8String,
  opaque            UTF8String,
  identifier      [0]   UTF8String OPTIONAL,
}
*/

typedef struct DigestInitReply {
  heim_utf8_string nonce;
  heim_utf8_string opaque;
  heim_utf8_string *identifier;
} DigestInitReply;

int    encode_DigestInitReply(unsigned char *, size_t, const DigestInitReply *, size_t *);
int    decode_DigestInitReply(const unsigned char *, size_t, DigestInitReply *, size_t *);
void   free_DigestInitReply  (DigestInitReply *);
size_t length_DigestInitReply(const DigestInitReply *);
int    copy_DigestInitReply  (const DigestInitReply *, DigestInitReply *);

/*
DigestRequest ::= SEQUENCE {
  type                    UTF8String,
  digest                  UTF8String,
  username                UTF8String,
  responseData            UTF8String,
  authid                [0]   UTF8String OPTIONAL,
  authentication-user   [1] Principal OPTIONAL,
  realm                 [2]   UTF8String OPTIONAL,
  method                [3]   UTF8String OPTIONAL,
  uri                   [4]   UTF8String OPTIONAL,
  serverNonce             UTF8String,
  clientNonce           [5]   UTF8String OPTIONAL,
  nonceCount            [6]   UTF8String OPTIONAL,
  qop                   [7]   UTF8String OPTIONAL,
  identifier            [8]   UTF8String OPTIONAL,
  hostname              [9]   UTF8String OPTIONAL,
  opaque                  UTF8String,
}
*/

typedef struct DigestRequest {
  heim_utf8_string type;
  heim_utf8_string digest;
  heim_utf8_string username;
  heim_utf8_string responseData;
  heim_utf8_string *authid;
  Principal *authentication_user;
  heim_utf8_string *realm;
  heim_utf8_string *method;
  heim_utf8_string *uri;
  heim_utf8_string serverNonce;
  heim_utf8_string *clientNonce;
  heim_utf8_string *nonceCount;
  heim_utf8_string *qop;
  heim_utf8_string *identifier;
  heim_utf8_string *hostname;
  heim_utf8_string opaque;
} DigestRequest;

int    encode_DigestRequest(unsigned char *, size_t, const DigestRequest *, size_t *);
int    decode_DigestRequest(const unsigned char *, size_t, DigestRequest *, size_t *);
void   free_DigestRequest  (DigestRequest *);
size_t length_DigestRequest(const DigestRequest *);
int    copy_DigestRequest  (const DigestRequest *, DigestRequest *);

/*
DigestError ::= SEQUENCE {
  reason            UTF8String,
  code            INTEGER (-2147483648..2147483647),
}
*/

typedef struct DigestError {
  heim_utf8_string reason;
  int code;
} DigestError;

int    encode_DigestError(unsigned char *, size_t, const DigestError *, size_t *);
int    decode_DigestError(const unsigned char *, size_t, DigestError *, size_t *);
void   free_DigestError  (DigestError *);
size_t length_DigestError(const DigestError *);
int    copy_DigestError  (const DigestError *, DigestError *);

/*
DigestResponse ::= SEQUENCE {
  success         BOOLEAN,
  rsp             [0]   UTF8String OPTIONAL,
  tickets         [1] SEQUENCE OF OCTET STRING OPTIONAL,
  channel         [2] SEQUENCE {
    cb-type             UTF8String,
    cb-binding          UTF8String,
  } OPTIONAL,
  session-key     [3] OCTET STRING OPTIONAL,
}
*/

typedef struct DigestResponse {
  int success;
  heim_utf8_string *rsp;
  struct  {
    unsigned int len;
    heim_octet_string *val;
  } *tickets;
  struct  {
    heim_utf8_string cb_type;
    heim_utf8_string cb_binding;
  } *channel;
  heim_octet_string *session_key;
} DigestResponse;

int    encode_DigestResponse(unsigned char *, size_t, const DigestResponse *, size_t *);
int    decode_DigestResponse(const unsigned char *, size_t, DigestResponse *, size_t *);
void   free_DigestResponse  (DigestResponse *);
size_t length_DigestResponse(const DigestResponse *);
int    copy_DigestResponse  (const DigestResponse *, DigestResponse *);

/*
NTLMInit ::= SEQUENCE {
  flags           [0] INTEGER (0..-1),
  hostname        [1]   UTF8String OPTIONAL,
  domain          [1]   UTF8String OPTIONAL,
}
*/

typedef struct NTLMInit {
  unsigned int flags;
  heim_utf8_string *hostname;
  heim_utf8_string *domain;
} NTLMInit;

int    encode_NTLMInit(unsigned char *, size_t, const NTLMInit *, size_t *);
int    decode_NTLMInit(const unsigned char *, size_t, NTLMInit *, size_t *);
void   free_NTLMInit  (NTLMInit *);
size_t length_NTLMInit(const NTLMInit *);
int    copy_NTLMInit  (const NTLMInit *, NTLMInit *);

/*
NTLMInitReply ::= SEQUENCE {
  flags           [0] INTEGER (0..-1),
  opaque          [1] OCTET STRING,
  targetname      [2]   UTF8String,
  challange       [3] OCTET STRING,
  targetinfo      [4] OCTET STRING OPTIONAL,
}
*/

typedef struct NTLMInitReply {
  unsigned int flags;
  heim_octet_string opaque;
  heim_utf8_string targetname;
  heim_octet_string challange;
  heim_octet_string *targetinfo;
} NTLMInitReply;

int    encode_NTLMInitReply(unsigned char *, size_t, const NTLMInitReply *, size_t *);
int    decode_NTLMInitReply(const unsigned char *, size_t, NTLMInitReply *, size_t *);
void   free_NTLMInitReply  (NTLMInitReply *);
size_t length_NTLMInitReply(const NTLMInitReply *);
int    copy_NTLMInitReply  (const NTLMInitReply *, NTLMInitReply *);

/*
NTLMRequest ::= SEQUENCE {
  flags           [0] INTEGER (0..-1),
  opaque          [1] OCTET STRING,
  username        [2]   UTF8String,
  targetname      [3]   UTF8String,
  targetinfo      [4] OCTET STRING OPTIONAL,
  lm              [5] OCTET STRING,
  ntlm            [6] OCTET STRING,
  sessionkey      [7] OCTET STRING OPTIONAL,
}
*/

typedef struct NTLMRequest {
  unsigned int flags;
  heim_octet_string opaque;
  heim_utf8_string username;
  heim_utf8_string targetname;
  heim_octet_string *targetinfo;
  heim_octet_string lm;
  heim_octet_string ntlm;
  heim_octet_string *sessionkey;
} NTLMRequest;

int    encode_NTLMRequest(unsigned char *, size_t, const NTLMRequest *, size_t *);
int    decode_NTLMRequest(const unsigned char *, size_t, NTLMRequest *, size_t *);
void   free_NTLMRequest  (NTLMRequest *);
size_t length_NTLMRequest(const NTLMRequest *);
int    copy_NTLMRequest  (const NTLMRequest *, NTLMRequest *);

/*
NTLMResponse ::= SEQUENCE {
  success         [0] BOOLEAN,
  flags           [1] INTEGER (0..-1),
  sessionkey      [2] OCTET STRING OPTIONAL,
  tickets         [3] SEQUENCE OF OCTET STRING OPTIONAL,
}
*/

typedef struct NTLMResponse {
  int success;
  unsigned int flags;
  heim_octet_string *sessionkey;
  struct  {
    unsigned int len;
    heim_octet_string *val;
  } *tickets;
} NTLMResponse;

int    encode_NTLMResponse(unsigned char *, size_t, const NTLMResponse *, size_t *);
int    decode_NTLMResponse(const unsigned char *, size_t, NTLMResponse *, size_t *);
void   free_NTLMResponse  (NTLMResponse *);
size_t length_NTLMResponse(const NTLMResponse *);
int    copy_NTLMResponse  (const NTLMResponse *, NTLMResponse *);

/*
DigestReqInner ::= CHOICE {
  init             [0] DigestInit,
  digestRequest    [1] DigestRequest,
  ntlmInit         [2] NTLMInit,
  ntlmRequest      [3] NTLMRequest,
  supportedMechs   [4]   NULL,
}
*/

typedef struct DigestReqInner {
  enum {
    choice_DigestReqInner_init = 1,
    choice_DigestReqInner_digestRequest,
    choice_DigestReqInner_ntlmInit,
    choice_DigestReqInner_ntlmRequest,
    choice_DigestReqInner_supportedMechs
  } element;
  union {
    DigestInit init;
    DigestRequest digestRequest;
    NTLMInit ntlmInit;
    NTLMRequest ntlmRequest;
    int supportedMechs;
  } u;
} DigestReqInner;

int    encode_DigestReqInner(unsigned char *, size_t, const DigestReqInner *, size_t *);
int    decode_DigestReqInner(const unsigned char *, size_t, DigestReqInner *, size_t *);
void   free_DigestReqInner  (DigestReqInner *);
size_t length_DigestReqInner(const DigestReqInner *);
int    copy_DigestReqInner  (const DigestReqInner *, DigestReqInner *);

/*
DigestREQ ::= [APPLICATION 128] SEQUENCE {
  apReq           [0] OCTET STRING,
  innerReq        [1] EncryptedData,
}
*/

typedef struct DigestREQ {
  heim_octet_string apReq;
  EncryptedData innerReq;
} DigestREQ;

int    encode_DigestREQ(unsigned char *, size_t, const DigestREQ *, size_t *);
int    decode_DigestREQ(const unsigned char *, size_t, DigestREQ *, size_t *);
void   free_DigestREQ  (DigestREQ *);
size_t length_DigestREQ(const DigestREQ *);
int    copy_DigestREQ  (const DigestREQ *, DigestREQ *);

/*
DigestRepInner ::= CHOICE {
  error            [0] DigestError,
  initReply        [1] DigestInitReply,
  response         [2] DigestResponse,
  ntlmInitReply    [3] NTLMInitReply,
  ntlmResponse     [4] NTLMResponse,
  supportedMechs   [5] DigestTypes,
  ...,
}
*/

typedef struct DigestRepInner {
  enum {
    choice_DigestRepInner_asn1_ellipsis = 0,
    choice_DigestRepInner_error,
    choice_DigestRepInner_initReply,
    choice_DigestRepInner_response,
    choice_DigestRepInner_ntlmInitReply,
    choice_DigestRepInner_ntlmResponse,
    choice_DigestRepInner_supportedMechs
    /* ... */
  } element;
  union {
    DigestError error;
    DigestInitReply initReply;
    DigestResponse response;
    NTLMInitReply ntlmInitReply;
    NTLMResponse ntlmResponse;
    DigestTypes supportedMechs;
    heim_octet_string asn1_ellipsis;
  } u;
} DigestRepInner;

int    encode_DigestRepInner(unsigned char *, size_t, const DigestRepInner *, size_t *);
int    decode_DigestRepInner(const unsigned char *, size_t, DigestRepInner *, size_t *);
void   free_DigestRepInner  (DigestRepInner *);
size_t length_DigestRepInner(const DigestRepInner *);
int    copy_DigestRepInner  (const DigestRepInner *, DigestRepInner *);

/*
DigestREP ::= [APPLICATION 129] SEQUENCE {
  apRep           [0] OCTET STRING,
  innerRep        [1] EncryptedData,
}
*/

typedef struct DigestREP {
  heim_octet_string apRep;
  EncryptedData innerRep;
} DigestREP;

int    encode_DigestREP(unsigned char *, size_t, const DigestREP *, size_t *);
int    decode_DigestREP(const unsigned char *, size_t, DigestREP *, size_t *);
void   free_DigestREP  (DigestREP *);
size_t length_DigestREP(const DigestREP *);
int    copy_DigestREP  (const DigestREP *, DigestREP *);

#endif /* __digest_asn1_h__ */


code:
/*-
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *
 *	@(#)dirent.h	8.2 (Berkeley) 7/28/94
 * $FreeBSD: release/9.0.0/include/dirent.h 203964 2010-02-16 19:39:50Z imp $
 */

#ifndef _DIRENT_H_
#define _DIRENT_H_

/*
 * The kernel defines the format of directory entries returned by
 * the getdirentries(2) system call.
 */
#include <sys/cdefs.h>
#include <sys/dirent.h>

#if __BSD_VISIBLE || __XSI_VISIBLE
/*
 * XXX this is probably illegal in the __XSI_VISIBLE case, but brings us closer
 * to the specification.
 */
#define	d_ino		d_fileno	/* backward and XSI compatibility */
#endif

#if __BSD_VISIBLE

#include <sys/_null.h>

/* definitions for library routines operating on directories. */
#define	DIRBLKSIZ	1024

struct _telldir;		/* see telldir.h */
struct pthread_mutex;

/* structure describing an open directory. */
typedef struct _dirdesc {
	int	dd_fd;		/* file descriptor associated with directory */
	long	dd_loc;		/* offset in current buffer */
	long	dd_size;	/* amount of data returned by getdirentries */
	char	*dd_buf;	/* data buffer */
	int	dd_len;		/* size of data buffer */
	long	dd_seek;	/* magic cookie returned by getdirentries */
	long	dd_rewind;	/* magic cookie for rewinding */
	int	dd_flags;	/* flags for readdir */
	struct pthread_mutex	*dd_lock;	/* lock */
	struct _telldir *dd_td;	/* telldir position recording */
} DIR;

#define	dirfd(dirp)	((dirp)->dd_fd)

/* flags for opendir2 */
#define DTF_HIDEW	0x0001	/* hide whiteout entries */
#define DTF_NODUP	0x0002	/* don't return duplicate names */
#define DTF_REWIND	0x0004	/* rewind after reading union stack */
#define __DTF_READALL	0x0008	/* everything has been read */

#else /* !__BSD_VISIBLE */

typedef	void *	DIR;

#endif /* __BSD_VISIBLE */

#ifndef _KERNEL

__BEGIN_DECLS
#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE >= 700
int	 alphasort(const struct dirent **, const struct dirent **);
#endif
#if __BSD_VISIBLE
DIR	*__opendir2(const char *, int);
int	 getdents(int, char *, int);
int	 getdirentries(int, char *, int, long *);
#endif
DIR	*opendir(const char *);
DIR	*fdopendir(int);
struct dirent *
	 readdir(DIR *);
#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE >= 500
int	 readdir_r(DIR *, struct dirent *, struct dirent **);
#endif
void	 rewinddir(DIR *);
#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE >= 700
int	 scandir(const char *, struct dirent ***,
	    int (*)(const struct dirent *), int (*)(const struct dirent **,
	    const struct dirent **));
#endif
#if __XSI_VISIBLE
void	 seekdir(DIR *, long);
long	 telldir(DIR *);
#endif
int	 closedir(DIR *);
__END_DECLS

#endif /* !_KERNEL */

#endif /* !_DIRENT_H_ */


code:
/*-
 * Copyright (c) 1994
 *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *
 * $FreeBSD: release/9.0.0/include/dlfcn.h 205606 2010-03-24 15:59:51Z gahr $
 */

#ifndef _DLFCN_H_
#define	_DLFCN_H_

#include <sys/_types.h>

/*
 * Modes and flags for dlopen().
 */
#define	RTLD_LAZY	1	/* Bind function calls lazily. */
#define	RTLD_NOW	2	/* Bind function calls immediately. */
#define	RTLD_MODEMASK	0x3
#define	RTLD_GLOBAL	0x100	/* Make symbols globally available. */
#define	RTLD_LOCAL	0	/* Opposite of RTLD_GLOBAL, and the default. */
#define	RTLD_TRACE	0x200	/* Trace loaded objects and exit. */
#define	RTLD_NODELETE	0x01000	/* Do not remove members. */
#define	RTLD_NOLOAD	0x02000	/* Do not load if not already loaded. */

/*
 * Request arguments for dlinfo().
 */
#define	RTLD_DI_LINKMAP		2	/* Obtain link map. */
#define	RTLD_DI_SERINFO		4	/* Obtain search path info. */
#define	RTLD_DI_SERINFOSIZE	5	/*  ... query for required space. */
#define	RTLD_DI_ORIGIN		6	/* Obtain object origin */
#define	RTLD_DI_MAX		RTLD_DI_ORIGIN

/*
 * Special handle arguments for dlsym()/dlinfo().
 */
#define	RTLD_NEXT	((void *) -1)	/* Search subsequent objects. */
#define	RTLD_DEFAULT	((void *) -2)	/* Use default search algorithm. */
#define	RTLD_SELF	((void *) -3)	/* Search the caller itself. */

#if __BSD_VISIBLE

#ifndef	_SIZE_T_DECLARED
typedef __size_t        size_t;
#define	_SIZE_T_DECLARED
#endif

/*
 * Structure filled in by dladdr().
 */
typedef	struct dl_info {
	const char	*dli_fname;	/* Pathname of shared object. */
	void		*dli_fbase;	/* Base address of shared object. */
	const char	*dli_sname;	/* Name of nearest symbol. */
	void		*dli_saddr;	/* Address of nearest symbol. */
} Dl_info;

/*-
 * The actual type declared by this typedef is immaterial, provided that
 * it is a function pointer.  Its purpose is to provide a return type for
 * dlfunc() which can be cast to a function pointer type without depending
 * on behavior undefined by the C standard, which might trigger a compiler
 * diagnostic.  We intentionally declare a unique type signature to force
 * a diagnostic should the application not cast the return value of dlfunc()
 * appropriately.
 */
struct __dlfunc_arg {
	int	__dlfunc_dummy;
};

typedef	void (*dlfunc_t)(struct __dlfunc_arg);

/*
 * Structures, returned by the RTLD_DI_SERINFO dlinfo() request.
 */
typedef struct dl_serpath {
	char *		dls_name;	/* single search path entry */
	unsigned int	dls_flags;	/* path information */
} Dl_serpath;

typedef struct  dl_serinfo {
        size_t		dls_size;       /* total buffer size */
        unsigned int	dls_cnt;        /* number of path entries */
        Dl_serpath	dls_serpath[1]; /* there may be more than one */
} Dl_serinfo;

#endif /* __BSD_VISIBLE */

__BEGIN_DECLS
/* XSI functions first. */
int	 dlclose(void *);
char	*dlerror(void);
void	*dlopen(const char *, int);
void	*dlsym(void * __restrict, const char * __restrict);

#if __BSD_VISIBLE
int	 dladdr(const void * __restrict, Dl_info * __restrict);
dlfunc_t dlfunc(void * __restrict, const char * __restrict);
int	 dlinfo(void * __restrict, int, void * __restrict);
void	 dllockinit(void *_context,
	    void *(*_lock_create)(void *_context),
	    void (*_rlock_acquire)(void *_lock),
	    void (*_wlock_acquire)(void *_lock),
	    void (*_lock_release)(void *_lock),
	    void (*_lock_destroy)(void *_lock),
	    void (*_context_destroy)(void *_context));
void	*dlvsym(void * __restrict, const char * __restrict,
	    const char * __restrict);
#endif /* __BSD_VISIBLE */
__END_DECLS

#endif /* !_DLFCN_H_ */