| 1 |
/* |
| 2 |
* Copyright (c) 2001-2002 Michael David Adams. |
| 3 |
* All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
/* __START_OF_JASPER_LICENSE__ |
| 7 |
* |
| 8 |
* JasPer Software License |
| 9 |
* |
| 10 |
* IMAGE POWER JPEG-2000 PUBLIC LICENSE |
| 11 |
* ************************************ |
| 12 |
* |
| 13 |
* GRANT: |
| 14 |
* |
| 15 |
* Permission is hereby granted, free of charge, to any person (the "User") |
| 16 |
* obtaining a copy of this software and associated documentation, to deal |
| 17 |
* in the JasPer Software without restriction, including without limitation |
| 18 |
* the right to use, copy, modify, merge, publish, distribute, sublicense, |
| 19 |
* and/or sell copies of the JasPer Software (in source and binary forms), |
| 20 |
* and to permit persons to whom the JasPer Software is furnished to do so, |
| 21 |
* provided further that the License Conditions below are met. |
| 22 |
* |
| 23 |
* License Conditions |
| 24 |
* ****************** |
| 25 |
* |
| 26 |
* A. Redistributions of source code must retain the above copyright notice, |
| 27 |
* and this list of conditions, and the following disclaimer. |
| 28 |
* |
| 29 |
* B. Redistributions in binary form must reproduce the above copyright |
| 30 |
* notice, and this list of conditions, and the following disclaimer in |
| 31 |
* the documentation and/or other materials provided with the distribution. |
| 32 |
* |
| 33 |
* C. Neither the name of Image Power, Inc. nor any other contributor |
| 34 |
* (including, but not limited to, the University of British Columbia and |
| 35 |
* Michael David Adams) may be used to endorse or promote products derived |
| 36 |
* from this software without specific prior written permission. |
| 37 |
* |
| 38 |
* D. User agrees that it shall not commence any action against Image Power, |
| 39 |
* Inc., the University of British Columbia, Michael David Adams, or any |
| 40 |
* other contributors (collectively "Licensors") for infringement of any |
| 41 |
* intellectual property rights ("IPR") held by the User in respect of any |
| 42 |
* technology that User owns or has a right to license or sublicense and |
| 43 |
* which is an element required in order to claim compliance with ISO/IEC |
| 44 |
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property |
| 45 |
* rights worldwide arising under statutory or common law, and whether |
| 46 |
* or not perfected, including, without limitation, all (i) patents and |
| 47 |
* patent applications owned or licensable by User; (ii) rights associated |
| 48 |
* with works of authorship including copyrights, copyright applications, |
| 49 |
* copyright registrations, mask work rights, mask work applications, |
| 50 |
* mask work registrations; (iii) rights relating to the protection of |
| 51 |
* trade secrets and confidential information; (iv) any right analogous |
| 52 |
* to those set forth in subsections (i), (ii), or (iii) and any other |
| 53 |
* proprietary rights relating to intangible property (other than trademark, |
| 54 |
* trade dress, or service mark rights); and (v) divisions, continuations, |
| 55 |
* renewals, reissues and extensions of the foregoing (as and to the extent |
| 56 |
* applicable) now existing, hereafter filed, issued or acquired. |
| 57 |
* |
| 58 |
* E. If User commences an infringement action against any Licensor(s) then |
| 59 |
* such Licensor(s) shall have the right to terminate User's license and |
| 60 |
* all sublicenses that have been granted hereunder by User to other parties. |
| 61 |
* |
| 62 |
* F. This software is for use only in hardware or software products that |
| 63 |
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license |
| 64 |
* or right to this Software is granted for products that do not comply |
| 65 |
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased |
| 66 |
* from the ISO. |
| 67 |
* |
| 68 |
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. |
| 69 |
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
| 70 |
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND |
| 71 |
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY |
| 72 |
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, |
| 73 |
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, |
| 74 |
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING |
| 75 |
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE |
| 76 |
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING |
| 77 |
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. |
| 78 |
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE |
| 79 |
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE |
| 80 |
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY |
| 81 |
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY |
| 82 |
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, |
| 83 |
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, |
| 84 |
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE |
| 85 |
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., |
| 86 |
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE |
| 87 |
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO |
| 88 |
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR |
| 89 |
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, |
| 90 |
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR |
| 91 |
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF |
| 92 |
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY |
| 93 |
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT |
| 94 |
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR |
| 95 |
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING |
| 96 |
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, |
| 97 |
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT |
| 98 |
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE |
| 99 |
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY |
| 100 |
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE |
| 101 |
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS |
| 102 |
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT |
| 103 |
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING |
| 104 |
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS |
| 105 |
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE |
| 106 |
* NOTICE SPECIFIED IN THIS SECTION. |
| 107 |
* |
| 108 |
* __END_OF_JASPER_LICENSE__ |
| 109 |
*/ |
| 110 |
|
| 111 |
/* |
| 112 |
* Tag-Value Parser Library |
| 113 |
* |
| 114 |
* $Id$ |
| 115 |
*/ |
| 116 |
|
| 117 |
/******************************************************************************\ |
| 118 |
* Includes. |
| 119 |
\******************************************************************************/ |
| 120 |
|
| 121 |
#include <assert.h> |
| 122 |
#include <stdio.h> |
| 123 |
#include <ctype.h> |
| 124 |
#include <stdlib.h> |
| 125 |
|
| 126 |
#include "jasper/jas_malloc.h" |
| 127 |
#include "jasper/jas_string.h" |
| 128 |
#include "jasper/jas_tvp.h" |
| 129 |
|
| 130 |
/******************************************************************************\ |
| 131 |
* Macros. |
| 132 |
\******************************************************************************/ |
| 133 |
|
| 134 |
/* Is the specified character valid for a tag name? */ |
| 135 |
#define JAS_TVP_ISTAG(x) \ |
| 136 |
(isalpha(x) || (x) == '_' || isdigit(x)) |
| 137 |
|
| 138 |
/******************************************************************************\ |
| 139 |
* Code for creating and destroying a tag-value parser. |
| 140 |
\******************************************************************************/ |
| 141 |
|
| 142 |
jas_tvparser_t *jas_tvparser_create(const char *s) |
| 143 |
{ |
| 144 |
jas_tvparser_t *tvp; |
| 145 |
if (!(tvp = jas_malloc(sizeof(jas_tvparser_t)))) { |
| 146 |
return 0; |
| 147 |
} |
| 148 |
if (!(tvp->buf = jas_strdup(s))) { |
| 149 |
jas_tvparser_destroy(tvp); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
tvp->pos = tvp->buf; |
| 153 |
tvp->tag = 0; |
| 154 |
tvp->val = 0; |
| 155 |
return tvp; |
| 156 |
} |
| 157 |
|
| 158 |
void jas_tvparser_destroy(jas_tvparser_t *tvp) |
| 159 |
{ |
| 160 |
if (tvp->buf) { |
| 161 |
jas_free(tvp->buf); |
| 162 |
} |
| 163 |
jas_free(tvp); |
| 164 |
} |
| 165 |
|
| 166 |
/******************************************************************************\ |
| 167 |
* Main parsing code. |
| 168 |
\******************************************************************************/ |
| 169 |
|
| 170 |
/* Get the next tag-value pair. */ |
| 171 |
int jas_tvparser_next(jas_tvparser_t *tvp) |
| 172 |
{ |
| 173 |
char *p; |
| 174 |
char *tag; |
| 175 |
const char *val; |
| 176 |
|
| 177 |
/* Skip any leading whitespace. */ |
| 178 |
p = tvp->pos; |
| 179 |
while (*p != '\0' && isspace(*p)) { |
| 180 |
++p; |
| 181 |
} |
| 182 |
|
| 183 |
/* Has the end of the input data been reached? */ |
| 184 |
if (*p == '\0') { |
| 185 |
/* No more tags are present. */ |
| 186 |
tvp->pos = p; |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
/* Does the tag name begin with a valid character? */ |
| 191 |
if (!JAS_TVP_ISTAG(*p)) { |
| 192 |
return -1; |
| 193 |
} |
| 194 |
|
| 195 |
/* Remember where the tag name begins. */ |
| 196 |
tag = p; |
| 197 |
|
| 198 |
/* Find the end of the tag name. */ |
| 199 |
while (*p != '\0' && JAS_TVP_ISTAG(*p)) { |
| 200 |
++p; |
| 201 |
} |
| 202 |
|
| 203 |
/* Has the end of the input data been reached? */ |
| 204 |
if (*p == '\0') { |
| 205 |
/* The value field is empty. */ |
| 206 |
tvp->tag = tag; |
| 207 |
tvp->val = ""; |
| 208 |
tvp->pos = p; |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
/* Is a value field not present? */ |
| 213 |
if (*p != '=') { |
| 214 |
if (*p != '\0' && !isspace(*p)) { |
| 215 |
return -1; |
| 216 |
} |
| 217 |
*p++ = '\0'; |
| 218 |
tvp->tag = tag; |
| 219 |
tvp->val = ""; |
| 220 |
tvp->pos = p; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
*p++ = '\0'; |
| 225 |
|
| 226 |
val = p; |
| 227 |
while (*p != '\0' && !isspace(*p)) { |
| 228 |
++p; |
| 229 |
} |
| 230 |
|
| 231 |
if (*p != '\0') { |
| 232 |
*p++ = '\0'; |
| 233 |
} |
| 234 |
|
| 235 |
tvp->pos = p; |
| 236 |
tvp->tag = tag; |
| 237 |
tvp->val = val; |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
/******************************************************************************\ |
| 243 |
* Code for querying the current tag/value. |
| 244 |
\******************************************************************************/ |
| 245 |
|
| 246 |
/* Get the current tag. */ |
| 247 |
char *jas_tvparser_gettag(jas_tvparser_t *tvp) |
| 248 |
{ |
| 249 |
return tvp->tag; |
| 250 |
} |
| 251 |
|
| 252 |
/* Get the current value. */ |
| 253 |
const char *jas_tvparser_getval(jas_tvparser_t *tvp) |
| 254 |
{ |
| 255 |
return tvp->val; |
| 256 |
} |
| 257 |
|
| 258 |
/******************************************************************************\ |
| 259 |
* Miscellaneous code. |
| 260 |
\******************************************************************************/ |
| 261 |
|
| 262 |
/* Lookup a tag by name. */ |
| 263 |
jas_taginfo_t *jas_taginfos_lookup(jas_taginfo_t *taginfos, const char *name) |
| 264 |
{ |
| 265 |
jas_taginfo_t *taginfo; |
| 266 |
taginfo = taginfos; |
| 267 |
while (taginfo->id >= 0) { |
| 268 |
if (!strcmp(taginfo->name, name)) { |
| 269 |
return taginfo; |
| 270 |
} |
| 271 |
++taginfo; |
| 272 |
} |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
/* This function is simply for convenience. */ |
| 277 |
/* One can avoid testing for the special case of a null pointer, by |
| 278 |
using this function. This function never returns a null pointer. */ |
| 279 |
jas_taginfo_t *jas_taginfo_nonull(jas_taginfo_t *taginfo) |
| 280 |
{ |
| 281 |
static jas_taginfo_t invalidtaginfo = { |
| 282 |
-1, 0 |
| 283 |
}; |
| 284 |
|
| 285 |
return taginfo ? taginfo : &invalidtaginfo; |
| 286 |
} |