1139749Simp/*
272017Scg * Copyright (c) 2001-2002 Michael David Adams.
372017Scg * All rights reserved.
472017Scg */
572017Scg
672017Scg/* __START_OF_JASPER_LICENSE__
772017Scg *
872017Scg * JasPer Software License
972017Scg *
1072017Scg * IMAGE POWER JPEG-2000 PUBLIC LICENSE
1172017Scg * ************************************
1272017Scg *
1372017Scg * GRANT:
1472017Scg *
1572017Scg * Permission is hereby granted, free of charge, to any person (the "User")
1672017Scg * obtaining a copy of this software and associated documentation, to deal
1772017Scg * in the JasPer Software without restriction, including without limitation
1872017Scg * the right to use, copy, modify, merge, publish, distribute, sublicense,
1972017Scg * and/or sell copies of the JasPer Software (in source and binary forms),
2072017Scg * and to permit persons to whom the JasPer Software is furnished to do so,
2172017Scg * provided further that the License Conditions below are met.
2272017Scg *
2372017Scg * License Conditions
2472017Scg * ******************
2572017Scg *
2672017Scg * A.  Redistributions of source code must retain the above copyright notice,
2772017Scg * and this list of conditions, and the following disclaimer.
2872455Scg *
2972455Scg * B.  Redistributions in binary form must reproduce the above copyright
3072455Scg * notice, and this list of conditions, and the following disclaimer in
3172017Scg * the documentation and/or other materials provided with the distribution.
3272017Scg *
3372017Scg * C.  Neither the name of Image Power, Inc. nor any other contributor
3472017Scg * (including, but not limited to, the University of British Columbia and
35119287Simp * Michael David Adams) may be used to endorse or promote products derived
36119287Simp * from this software without specific prior written permission.
3772017Scg *
3872017Scg * D.  User agrees that it shall not commence any action against Image Power,
3972017Scg * Inc., the University of British Columbia, Michael David Adams, or any
4082180Scg * other contributors (collectively "Licensors") for infringement of any
4182180Scg * intellectual property rights ("IPR") held by the User in respect of any
4284771Sorion * technology that User owns or has a right to license or sublicense and
4372017Scg * which is an element required in order to claim compliance with ISO/IEC
4472017Scg * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
4572017Scg * rights worldwide arising under statutory or common law, and whether
4672017Scg * or not perfected, including, without limitation, all (i) patents and
4772017Scg * patent applications owned or licensable by User; (ii) rights associated
4872017Scg * with works of authorship including copyrights, copyright applications,
4972017Scg * copyright registrations, mask work rights, mask work applications,
5072017Scg * mask work registrations; (iii) rights relating to the protection of
5172017Scg * trade secrets and confidential information; (iv) any right analogous
5272017Scg * to those set forth in subsections (i), (ii), or (iii) and any other
5372017Scg * proprietary rights relating to intangible property (other than trademark,
5472017Scg * trade dress, or service mark rights); and (v) divisions, continuations,
5572017Scg * renewals, reissues and extensions of the foregoing (as and to the extent
5672017Scg * applicable) now existing, hereafter filed, issued or acquired.
5772017Scg *
5872017Scg * E.  If User commences an infringement action against any Licensor(s) then
5972017Scg * such Licensor(s) shall have the right to terminate User's license and
6072017Scg * all sublicenses that have been granted hereunder by User to other parties.
6172017Scg *
6272017Scg * F.  This software is for use only in hardware or software products that
6372017Scg * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
6472017Scg * or right to this Software is granted for products that do not comply
6572017Scg * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
6672017Scg * from the ISO.
6772017Scg *
6874763Scg * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
6974763Scg * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
7072017Scg * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
7172455Scg * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
7272455Scg * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
7372455Scg * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
7472017Scg * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
7572017Scg * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
7672017Scg * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
7772017Scg * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
7872017Scg * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
7972017Scg * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
8072017Scg * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
8172017Scg * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
8272017Scg * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
8372017Scg * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
8472017Scg * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
8572017Scg * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
8672017Scg * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
8772017Scg * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
8872017Scg * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
8972017Scg * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
9084771Sorion * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
9172017Scg * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
9272017Scg * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
9372017Scg * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
9472017Scg * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
9572017Scg * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
9672017Scg * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
9772017Scg * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
9872017Scg * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
9972017Scg * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
10072017Scg * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
10172017Scg * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
10272455Scg * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
10372017Scg * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
10472017Scg * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
10572017Scg * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
10672017Scg * NOTICE SPECIFIED IN THIS SECTION.
10772017Scg *
10872017Scg * __END_OF_JASPER_LICENSE__
10972017Scg */
11072017Scg
11172017Scg/*
11272017Scg * Tag-Value Parser Library
11372017Scg *
11472017Scg * $Id: jas_tvp.c 14449 2005-10-20 12:15:56Z stippi $
11572017Scg */
11672017Scg
11772017Scg/******************************************************************************\
11872017Scg* Includes.
11972017Scg\******************************************************************************/
12072017Scg
12172017Scg#include <assert.h>
12272017Scg#include <stdio.h>
12372017Scg#include <ctype.h>
12472017Scg#include <stdlib.h>
12572017Scg
12672017Scg#include "jasper/jas_malloc.h"
12772017Scg#include "jasper/jas_string.h"
12872017Scg#include "jasper/jas_tvp.h"
12972017Scg
13072017Scg/******************************************************************************\
13172017Scg* Macros.
13272017Scg\******************************************************************************/
13372017Scg
13472017Scg/* Is the specified character valid for a tag name? */
13574763Scg#define	JAS_TVP_ISTAG(x) \
13672017Scg	(isalpha(x) || (x) == '_' || isdigit(x))
13772017Scg
13872017Scg/******************************************************************************\
13972017Scg* Code for creating and destroying a tag-value parser.
14072017Scg\******************************************************************************/
14172017Scg
14272017Scgjas_tvparser_t *jas_tvparser_create(const char *s)
14372017Scg{
14472017Scg	jas_tvparser_t *tvp;
14572017Scg	if (!(tvp = jas_malloc(sizeof(jas_tvparser_t)))) {
14672017Scg		return 0;
14772017Scg	}
14872017Scg	if (!(tvp->buf = jas_strdup(s))) {
14972017Scg		jas_tvparser_destroy(tvp);
15072017Scg		return 0;
15172017Scg	}
15272017Scg	tvp->pos = tvp->buf;
15372017Scg	tvp->tag = 0;
15472017Scg	tvp->val = 0;
15572017Scg	return tvp;
15672017Scg}
15772017Scg
15872017Scgvoid jas_tvparser_destroy(jas_tvparser_t *tvp)
15972017Scg{
16072017Scg	if (tvp->buf) {
16172017Scg		jas_free(tvp->buf);
16272017Scg	}
16372017Scg	jas_free(tvp);
16472017Scg}
16572017Scg
16672017Scg/******************************************************************************\
16772017Scg* Main parsing code.
16872017Scg\******************************************************************************/
16972017Scg
17072017Scg/* Get the next tag-value pair. */
17172017Scgint jas_tvparser_next(jas_tvparser_t *tvp)
17272017Scg{
17372017Scg	char *p;
17472017Scg	char *tag;
17572017Scg	char *val;
17672017Scg
17772017Scg	/* Skip any leading whitespace. */
17872017Scg	p = tvp->pos;
17972017Scg	while (*p != '\0' && isspace(*p)) {
18072017Scg		++p;
18172017Scg	}
18272017Scg
18372017Scg	/* Has the end of the input data been reached? */
18472017Scg	if (*p == '\0') {
18572017Scg		/* No more tags are present. */
18672017Scg		tvp->pos = p;
18772017Scg		return 1;
18872017Scg	}
18972017Scg
19072017Scg	/* Does the tag name begin with a valid character? */
19172017Scg	if (!JAS_TVP_ISTAG(*p)) {
19272017Scg		return -1;
19372017Scg	}
19472017Scg
19572017Scg	/* Remember where the tag name begins. */
19672017Scg	tag = p;
19772017Scg
19872017Scg	/* Find the end of the tag name. */
19972017Scg	while (*p != '\0' && JAS_TVP_ISTAG(*p)) {
20072017Scg		++p;
20172017Scg	}
20272017Scg
20372455Scg	/* Has the end of the input data been reached? */
20472017Scg	if (*p == '\0') {
20572017Scg		/* The value field is empty. */
20672017Scg		tvp->tag = tag;
20772017Scg		tvp->val = "";
20872017Scg		tvp->pos = p;
20972017Scg		return 0;
21072017Scg	}
21172017Scg
21272017Scg	/* Is a value field not present? */
21372017Scg	if (*p != '=') {
21472017Scg		if (*p != '\0' && !isspace(*p)) {
21572017Scg			return -1;
21672017Scg		}
21772017Scg		*p++ = '\0';
21872017Scg		tvp->tag = tag;
21972017Scg		tvp->val = "";
22072017Scg		tvp->pos = p;
22172017Scg		return 0;
22272017Scg	}
22372017Scg
22472017Scg	*p++ = '\0';
22572017Scg
22672017Scg	val = p;
22772017Scg	while (*p != '\0' && !isspace(*p)) {
22872455Scg		++p;
22972017Scg	}
23072017Scg
23172017Scg	if (*p != '\0') {
23272017Scg		*p++ = '\0';
23372017Scg	}
23472017Scg
23572017Scg	tvp->pos = p;
23672455Scg	tvp->tag = tag;
23772017Scg	tvp->val = val;
23872017Scg
23972455Scg	return 0;
24072017Scg}
24172017Scg
24272017Scg/******************************************************************************\
24372017Scg* Code for querying the current tag/value.
24472017Scg\******************************************************************************/
24572017Scg
24672017Scg/* Get the current tag. */
24772017Scgchar *jas_tvparser_gettag(jas_tvparser_t *tvp)
24872017Scg{
24972017Scg	return tvp->tag;
25072017Scg}
25172017Scg
25272455Scg/* Get the current value. */
25372017Scgchar *jas_tvparser_getval(jas_tvparser_t *tvp)
25472017Scg{
25572017Scg	return tvp->val;
25672017Scg}
25772455Scg
25872017Scg/******************************************************************************\
25972017Scg* Miscellaneous code.
26072017Scg\******************************************************************************/
26172017Scg
26272455Scg/* Lookup a tag by name. */
26372455Scgjas_taginfo_t *jas_taginfos_lookup(jas_taginfo_t *taginfos, const char *name)
26472017Scg{
26572017Scg	jas_taginfo_t *taginfo;
26672017Scg	taginfo = taginfos;
26772017Scg	while (taginfo->id >= 0) {
26872017Scg		if (!strcmp(taginfo->name, name)) {
26972017Scg			return taginfo;
27072017Scg		}
27172017Scg		++taginfo;
27272017Scg	}
27372017Scg	return 0;
27472017Scg}
27572017Scg
27672017Scg/* This function is simply for convenience. */
27772455Scg/* One can avoid testing for the special case of a null pointer, by
27872455Scg  using this function.   This function never returns a null pointer.  */
27972017Scgjas_taginfo_t *jas_taginfo_nonull(jas_taginfo_t *taginfo)
28072017Scg{
28172017Scg	static jas_taginfo_t invalidtaginfo = {
28272017Scg		-1, 0
28372017Scg	};
28472017Scg
28572017Scg	return taginfo ? taginfo : &invalidtaginfo;
28672455Scg}
28772017Scg