1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF. The full HDF copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hrepack.h"
#include "hrepack_parse.h"
#ifdef H4_HAVE_LIBSZ
#include "szlib.h"
#endif
/*-------------------------------------------------------------------------
* Function: parse_comp
*
* Purpose: read compression info
*
* Return: a list of names, the number of names and its compression type
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: July 15, 2003
*
*-------------------------------------------------------------------------
*/
obj_list_t *
parse_comp(const char *str, int *n_objs, comp_info_t *comp)
{
unsigned i, u;
char c;
size_t len = strlen(str);
int j, m, n, k, end_obj = -1, no_param = 0, l;
char obj[H4_MAX_NC_NAME];
char scomp[10];
char stype[5];
char smask[3];
obj_list_t *obj_list = NULL;
/* check for the end of object list and number of objects */
for (i = 0, n = 0; i < len; i++) {
c = str[i];
if (c == ':') {
end_obj = i;
}
if (c == ',') {
n++;
}
}
if (end_obj == -1) { /* missing : */
printf("Input Error: Invalid compression input in <%s>\n", str);
return NULL;
}
/*-------------------------------------------------------------------------
* allocate the object list of names
*-------------------------------------------------------------------------
*/
n++;
obj_list = malloc(n * sizeof(obj_list_t));
*n_objs = n;
/* get object list */
for (j = 0, k = 0, n = 0; j < end_obj; j++, k++) {
c = str[j];
obj[k] = c;
if (c == ',' || j == end_obj - 1) {
if (c == ',')
obj[k] = '\0';
else
obj[k + 1] = '\0';
strcpy(obj_list[n].obj, obj);
memset(obj, 0, sizeof(obj));
n++;
k = -1;
}
}
/* nothing after : */
if (end_obj + 1 == (int)len) {
printf("Input Error: Invalid compression type in <%s>\n", str);
goto out;
}
/*-------------------------------------------------------------------------
* get compression type
*-------------------------------------------------------------------------
*/
m = 0;
for (i = end_obj + 1, k = 0; i < len; i++, k++) {
c = str[i];
scomp[k] = c;
if (c == ' ' || i == len - 1) {
if (c == ' ') /*one more parameter */
{
scomp[k] = '\0'; /*cut space */
/*
SZIP is a special case , it can be
SZIP=8,EC
SZIP=8,NN
*/
if (strcmp(scomp, "SZIP") == 0) {
l = -1; /* mask index check */
for (m = 0, u = i + 1; u < len; u++, m++) {
if (str[u] == ',') {
stype[m] = '\0'; /* end digit of szip */
l = 0; /* start EC or NN search */
u++; /* skip ',' */
}
c = str[u];
if (!isdigit(c) && l == -1) {
printf("Input Error: Compression parameter not digit in <%s>\n", str);
goto out;
}
if (l == -1)
stype[m] = c;
else {
smask[l] = c;
l++;
if (l == 2) {
smask[l] = '\0';
i = len - 1; /* end */
(*n_objs)--; /* we counted an extra ',' */
if (strcmp(smask, "NN") == 0)
comp->szip_mode = NN_MODE;
else if (strcmp(smask, "EC") == 0)
comp->szip_mode = EC_MODE;
else {
printf("Input Error: szip mask must be 'NN' or 'EC' \n");
goto out;
}
}
}
} /* u */
} /* SZIP */
else
{
/* here we could have 1, 2 or 3 digits (2 and 3 in the JPEG case) */
for (m = 0, u = i + 1; u < len; u++, m++) {
c = str[u];
if (!isdigit(c)) {
printf("Input Error: Compression parameter not digit in <%s>\n", str);
goto out;
}
stype[m] = c;
} /* m */
} /* else , no SZIP */
/* set return value of the compression parameter */
stype[m] = '\0';
comp->info = atoi(stype);
i += m; /* jump */
} /* if c==' ' */
else if (i == len - 1) { /*no more parameters */
scomp[k + 1] = '\0';
no_param = 1;
}
if (strcmp(scomp, "NONE") == 0)
comp->type = COMP_CODE_NONE;
else if (strcmp(scomp, "RLE") == 0) {
comp->type = COMP_CODE_RLE;
if (m > 0) { /*RLE does not have parameter */
printf("Input Error: Extra compression parameter in RLE <%s>\n", str);
goto out;
}
}
else if (strcmp(scomp, "HUFF") == 0) {
comp->type = COMP_CODE_SKPHUFF;
if (no_param) { /*no more parameters, HUFF must have parameter */
printf("Input Error: Missing compression parameter in <%s>\n", str);
goto out;
}
}
else if (strcmp(scomp, "GZIP") == 0) {
comp->type = COMP_CODE_DEFLATE;
if (no_param) { /*no more parameters, GZIP must have parameter */
printf("Input Error: Missing compression parameter in <%s>\n", str);
goto out;
}
}
else if (strcmp(scomp, "JPEG") == 0) {
comp->type = COMP_CODE_JPEG;
if (no_param) { /*no more parameters, JPEG must have parameter */
printf("Input Error: Missing compression parameter in <%s>\n", str);
goto out;
}
}
else if (strcmp(scomp, "SZIP") == 0) {
#ifdef H4_HAVE_LIBSZ
if (SZ_encoder_enabled()) {
comp->type = COMP_CODE_SZIP;
if (no_param) { /*no more parameters, SZIP must have parameter */
printf("Input Error: Missing compression parameter in <%s>\n", str);
goto out;
}
if (comp->szip_mode == FAIL) {
printf("Input Error: SZIP compression mode must be NN_MODE or EC_MODE");
goto out;
}
}
else {
printf("Input Error: SZIP encoder is not available\n");
goto out;
}
#else
printf("Input Error: SZIP compression is not available\n");
goto out;
#endif
}
else {
printf("Input Error: Invalid compression type in <%s>\n", str);
goto out;
}
}
} /*i*/
/*-------------------------------------------------------------------------
* check valid parameters
*-------------------------------------------------------------------------
*/
switch (comp->type) {
default:
break;
case COMP_CODE_RLE:
break;
case COMP_CODE_SKPHUFF:
if (comp->info <= 0) {
printf("Input Error: Invalid compression parameter in <%s>\n", str);
goto out;
}
break;
case COMP_CODE_DEFLATE:
if (comp->info < 0 || comp->info > 9) {
printf("Input Error: Invalid compression parameter in <%s>\n", str);
goto out;
}
break;
case COMP_CODE_JPEG:
if (comp->info < 0 || comp->info > 100) {
printf("Input Error: Invalid compression parameter in <%s>\n", str);
goto out;
}
break;
case COMP_CODE_SZIP:
#ifdef H4_HAVE_LIBSZ
if ((comp->info <= 1 || comp->info > SZ_MAX_PIXELS_PER_BLOCK) || (comp->info % 2 != 0)) {
printf("Input Error: Invalid compression parameter in <%s>. \
Pixels per block must be an even number < %d\n",
str, SZ_MAX_PIXELS_PER_BLOCK);
goto out;
}
#else
printf("Input Error: Invalid compression method in <%s>. SZIP is not available\n", str);
goto out;
#endif
break;
};
return obj_list;
out:
free(obj_list);
return NULL;
}
/*-------------------------------------------------------------------------
* Function: parse_chunk
*
* Purpose: read chunk info
*
* Return: a list of names, the number of names and its chunking info
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: July 17, 2003
*
*-------------------------------------------------------------------------
*/
obj_list_t *
parse_chunk(const char *str, int *n_objs, int32 *chunk_lengths, int *chunk_rank)
{
obj_list_t *obj_list = NULL;
unsigned i;
char c;
size_t len = strlen(str);
int j, n, k, end_obj = -1, c_index;
char obj[H4_MAX_NC_NAME];
char sdim[10];
/* check for the end of object list and number of objects */
for (i = 0, n = 0; i < len; i++) {
c = str[i];
if (c == ':') {
end_obj = i;
}
if (c == ',') {
n++;
}
}
if (end_obj == -1) { /* missing : */
printf("Input Error: Invalid chunking input in <%s>\n", str);
return NULL;
}
/*-------------------------------------------------------------------------
* allocate the object list of names
*-------------------------------------------------------------------------
*/
n++;
obj_list = malloc(n * sizeof(obj_list_t));
*n_objs = n;
/* get object list */
for (j = 0, k = 0, n = 0; j < end_obj; j++, k++) {
c = str[j];
obj[k] = c;
if (c == ',' || j == end_obj - 1) {
if (c == ',')
obj[k] = '\0';
else
obj[k + 1] = '\0';
strcpy(obj_list[n].obj, obj);
memset(obj, 0, sizeof(obj));
n++;
k = -1;
}
}
/* nothing after : */
if (end_obj + 1 == (int)len) {
printf("Input Error: Invalid chunking in <%s>\n", str);
goto out;
}
/* get chunk info */
k = 0;
for (i = end_obj + 1, c_index = 0; i < len; i++) {
c = str[i];
sdim[k] = c;
k++; /*increment sdim index */
if (!isdigit(c) && c != 'x' && c != 'N' && c != 'O' && c != 'N' && c != 'E') {
printf("Input Error: Invalid chunking in <%s>\n", str);
goto out;
}
if (c == 'x' || i == len - 1) {
if (c == 'x') {
sdim[k - 1] = '\0';
k = 0;
chunk_lengths[c_index] = atoi(sdim);
if (chunk_lengths[c_index] == 0) {
printf("Input Error: Invalid chunking in <%s>\n", str);
goto out;
}
c_index++;
}
else if (i == len - 1) { /*no more parameters */
sdim[k] = '\0';
k = 0;
if (strcmp(sdim, "NONE") == 0) {
*chunk_rank = -2;
}
else {
chunk_lengths[c_index] = atoi(sdim);
if (chunk_lengths[c_index] == 0) {
printf("Input Error: Invalid chunking in <%s>\n", str);
goto out;
}
*chunk_rank = c_index + 1;
}
} /*if */
} /*if c=='x' || i==len-1 */
} /*i*/
return obj_list;
out:
free(obj_list);
return NULL;
}
/*-------------------------------------------------------------------------
* Function: parse_number
*
* Purpose: read a number from command line argument
*
* Return: number, -1 for FAIL
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: July 31, 2003
*
*-------------------------------------------------------------------------
*/
int
parse_number(char *str)
{
unsigned i;
int n;
char c;
size_t len = strlen(str);
for (i = 0; i < len; i++) {
c = str[i];
if (!isdigit(c)) {
return -1;
}
}
str[i] = '\0';
n = atoi(str);
return n;
}
/*-------------------------------------------------------------------------
* Function: get_scomp
*
* Purpose: return the compression type as a string
*
*-------------------------------------------------------------------------
*/
const char *
get_scomp(comp_coder_t code)
{
if (code == COMP_CODE_RLE)
return "RLE";
else if (code == COMP_CODE_NBIT)
return "COMP_CODE_NBIT";
else if (code == COMP_CODE_SKPHUFF)
return "HUFF";
else if (code == COMP_CODE_DEFLATE)
return "GZIP";
else if (code == COMP_CODE_JPEG)
return "JPEG";
else if (code == COMP_CODE_SZIP)
return "SZIP";
else if (code == COMP_CODE_NONE)
return "NONE";
else if (code == COMP_CODE_INVALID)
return "COMP_CODE_INVALID";
else {
printf("Input Error in compression type\n");
return NULL;
}
return NULL;
}
|