| 1 |
/* |
| 2 |
Copyright (c) 1990-2007 Info-ZIP. All rights reserved. |
| 3 |
|
| 4 |
See the accompanying file LICENSE, version 2007-Mar-4 or later |
| 5 |
(the contents of which are also included in zip.h) for terms of use. |
| 6 |
If, for some reason, all these files are missing, the Info-ZIP license |
| 7 |
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html |
| 8 |
*/ |
| 9 |
/* |
| 10 |
* vms_im.c (zip) by Igor Mandrichenko Version 2.2-2 |
| 11 |
* |
| 12 |
* Revision history: |
| 13 |
* ... |
| 14 |
* 2.1-1 16-feb-1993 I.Mandrichenko |
| 15 |
* Get file size from XABFHC and check bytes rest in file before |
| 16 |
* reading. |
| 17 |
* 2.1-2 2-mar-1993 I.Mandrichenko |
| 18 |
* Make code more standard |
| 19 |
* 2.2 21-jun-1993 I.Mandrichenko |
| 20 |
* Free all allocated space, use more static storage. |
| 21 |
* Use memcompress() from bits.c (deflation) for block compression. |
| 22 |
* To revert to old compression method #define OLD_COMPRESS |
| 23 |
* 2.2-2 28-sep-1995 C.Spieler |
| 24 |
* Reorganized code for easier maintance of the two incompatible |
| 25 |
* flavours (IM style and PK style) VMS attribute support. |
| 26 |
* Generic functions (common to both flavours) are now collected |
| 27 |
* in a `wrapper' source file that includes one of the VMS attribute |
| 28 |
* handlers. |
| 29 |
* 3.0 23-Oct-2004 Steven Schweda |
| 30 |
* Changed to maintain compatibility with VMS_PK.C. Note that |
| 31 |
* reading with sys$read() prevents getting any data past EOF, |
| 32 |
* regardless of appearances. Moved the VMS_PK_EXTRA test into |
| 33 |
* here from VMS.C to allow more general automatic dependency |
| 34 |
* generation. |
| 35 |
* 17-Feb-2005 Steven Schweda |
| 36 |
* Added support for ODS5 extended names. |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifdef VMS /* For VMS only ! */ |
| 40 |
|
| 41 |
#ifndef VMS_PK_EXTRA |
| 42 |
|
| 43 |
#define OLD_COMPRESS /*To use old compression method define it.*/ |
| 44 |
|
| 45 |
#ifdef VMS_ZIP |
| 46 |
#undef VMS_ZIP /* do NOT include PK style Zip definitions */ |
| 47 |
#endif |
| 48 |
|
| 49 |
#include "vms.h" |
| 50 |
|
| 51 |
#ifndef __LIB$ROUTINES_LOADED |
| 52 |
#include <lib$routines.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef UTIL |
| 56 |
|
| 57 |
#define RET_ERROR 1 |
| 58 |
#define RET_SUCCESS 0 |
| 59 |
#define RET_EOF 0 |
| 60 |
|
| 61 |
#define Kbyte 1024 |
| 62 |
|
| 63 |
typedef struct XAB *xabptr; |
| 64 |
|
| 65 |
/* |
| 66 |
* Block sizes |
| 67 |
*/ |
| 68 |
|
| 69 |
#define EXTL0 ((FABL + EXTHL)+ \ |
| 70 |
(XFHCL + EXTHL)+ \ |
| 71 |
(XPROL + EXTHL)+ \ |
| 72 |
(XDATL + EXTHL)+ \ |
| 73 |
(XRDTL + EXTHL)) |
| 74 |
|
| 75 |
#ifdef OLD_COMPRESS |
| 76 |
#define PAD sizeof(uch) |
| 77 |
#else |
| 78 |
#define PAD 10*sizeof(ush) /* Two extra bytes for compr. header */ |
| 79 |
#endif |
| 80 |
|
| 81 |
#define PAD0 (5*PAD) /* Reserve space for the case when |
| 82 |
* compression fails */ |
| 83 |
static int _compress(uch *from, uch *to, int size); |
| 84 |
#ifdef DEBUG |
| 85 |
static void dump_rms_block(uch *p); |
| 86 |
#endif /* DEBUG */ |
| 87 |
|
| 88 |
/******************************** |
| 89 |
* Function set_extra_field * |
| 90 |
********************************/ |
| 91 |
/* |
| 92 |
| 2004-11-11 SMS. |
| 93 |
| Changed to use separate storage for ->extra and ->cextra. Zip64 |
| 94 |
| processing may move (reallocate) one and not the other. |
| 95 |
*/ |
| 96 |
|
| 97 |
static uch *_compress_block(register struct IZ_block *to, |
| 98 |
uch *from, int size, char *sig); |
| 99 |
static int get_vms_version(char *verbuf, int len); |
| 100 |
|
| 101 |
int set_extra_field(z, z_utim) |
| 102 |
struct zlist far *z; |
| 103 |
iztimes *z_utim; |
| 104 |
/* |
| 105 |
* Get file VMS file attributes and store them into extent fields. |
| 106 |
* Store VMS version also. |
| 107 |
* On error leave z intact. |
| 108 |
*/ |
| 109 |
{ |
| 110 |
int status; |
| 111 |
uch *xtra; |
| 112 |
uch *cxtra; |
| 113 |
uch *scan; |
| 114 |
extent extra_l; |
| 115 |
static struct FAB fab; |
| 116 |
static struct NAM_STRUCT nam; |
| 117 |
static struct XABSUM xabsum; |
| 118 |
static struct XABFHC xabfhc; |
| 119 |
static struct XABDAT xabdat; |
| 120 |
static struct XABPRO xabpro; |
| 121 |
static struct XABRDT xabrdt; |
| 122 |
xabptr x = (xabptr)NULL, xab_chain = (xabptr)NULL, last_xab = (xabptr)NULL; |
| 123 |
int nk, na; |
| 124 |
int i; |
| 125 |
int rc=RET_ERROR; |
| 126 |
char verbuf[80]; |
| 127 |
int verlen = 0; |
| 128 |
|
| 129 |
if (!vms_native) |
| 130 |
{ |
| 131 |
#ifdef USE_EF_UT_TIME |
| 132 |
/* |
| 133 |
* A `portable' zipfile entry is created. Create an "UT" extra block |
| 134 |
* containing UNIX style modification time stamp in UTC, which helps |
| 135 |
* maintaining the `real' "last modified" time when the archive is |
| 136 |
* transfered across time zone boundaries. |
| 137 |
*/ |
| 138 |
# ifdef IZ_CHECK_TZ |
| 139 |
if (!zp_tz_is_valid) |
| 140 |
return ZE_OK; /* skip silently if no valid TZ info */ |
| 141 |
# endif |
| 142 |
if ((xtra = (uch *) malloc( EB_HEADSIZE+ EB_UT_LEN( 1))) == NULL) |
| 143 |
return ZE_MEM; |
| 144 |
|
| 145 |
if ((cxtra = (uch *) malloc( EB_HEADSIZE+ EB_UT_LEN( 1))) == NULL) |
| 146 |
return ZE_MEM; |
| 147 |
|
| 148 |
/* Fill xtra[] with data. */ |
| 149 |
xtra[ 0] = 'U'; |
| 150 |
xtra[ 1] = 'T'; |
| 151 |
xtra[ 2] = EB_UT_LEN(1); /* length of data part of e.f. */ |
| 152 |
xtra[ 3] = 0; |
| 153 |
xtra[ 4] = EB_UT_FL_MTIME; |
| 154 |
xtra[ 5] = (uch) (z_utim->mtime); |
| 155 |
xtra[ 6] = (uch) (z_utim->mtime >> 8); |
| 156 |
xtra[ 7] = (uch) (z_utim->mtime >> 16); |
| 157 |
xtra[ 8] = (uch) (z_utim->mtime >> 24); |
| 158 |
|
| 159 |
/* Copy xtra[] data into cxtra[]. */ |
| 160 |
memcpy( cxtra, xtra, (EB_HEADSIZE+ EB_UT_LEN( 1))); |
| 161 |
|
| 162 |
/* Set sizes and pointers. */ |
| 163 |
z->cext = z->ext = (EB_HEADSIZE+ EB_UT_LEN( 1)); |
| 164 |
z->extra = (char *) xtra; |
| 165 |
z->cextra = (char *) cxtra; |
| 166 |
|
| 167 |
#endif /* USE_EF_UT_TIME */ |
| 168 |
|
| 169 |
return RET_SUCCESS; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Initialize RMS control blocks and link them |
| 174 |
*/ |
| 175 |
|
| 176 |
fab = cc$rms_fab; |
| 177 |
nam = CC_RMS_NAM; |
| 178 |
xabsum = cc$rms_xabsum; |
| 179 |
xabdat = cc$rms_xabdat; |
| 180 |
xabfhc = cc$rms_xabfhc; |
| 181 |
xabpro = cc$rms_xabpro; |
| 182 |
xabrdt = cc$rms_xabrdt; |
| 183 |
|
| 184 |
fab.FAB_NAM = &nam; |
| 185 |
fab.fab$l_xab = (char*)&xabsum; |
| 186 |
/* |
| 187 |
* Open the file and read summary information. |
| 188 |
*/ |
| 189 |
|
| 190 |
#ifdef NAML$C_MAXRSS |
| 191 |
|
| 192 |
fab.fab$l_dna = (char *) -1; /* Using NAML for default name. */ |
| 193 |
fab.fab$l_fna = (char *) -1; /* Using NAML for file name. */ |
| 194 |
|
| 195 |
#endif /* def NAML$C_MAXRSS */ |
| 196 |
|
| 197 |
FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = z->name; |
| 198 |
FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = strlen( z->name); |
| 199 |
|
| 200 |
#ifdef NAML$M_OPEN_SPECIAL |
| 201 |
/* 2007-02-28 SMS. |
| 202 |
* If processing symlinks as symlinks ("-y"), then $OPEN the |
| 203 |
* link, not the target file. |
| 204 |
* |
| 205 |
* (nam.naml$v_open_special gets us the symlink itself instead of |
| 206 |
* its target. fab.fab$v_bio is necessary to allow sys$open() to |
| 207 |
* work. Without it, you get status %x0001860c, "%RMS-F-ORG, |
| 208 |
* invalid file organization value".) |
| 209 |
*/ |
| 210 |
if (linkput) |
| 211 |
{ |
| 212 |
nam.naml$v_open_special = 1; |
| 213 |
fab.fab$v_bio = 1; |
| 214 |
} |
| 215 |
#endif /* def NAML$M_OPEN_SPECIAL */ |
| 216 |
|
| 217 |
status = sys$open(&fab); |
| 218 |
if (ERR(status)) |
| 219 |
{ |
| 220 |
#ifdef DEBUG |
| 221 |
printf("set_extra_field: sys$open for file %s:\n error status = %d\n", |
| 222 |
z->name, status); |
| 223 |
#endif |
| 224 |
goto err_exit; |
| 225 |
} |
| 226 |
|
| 227 |
nk = xabsum.xab$b_nok; |
| 228 |
na = xabsum.xab$b_noa; |
| 229 |
#ifdef DEBUG |
| 230 |
printf("%d keys, %d alls\n", nk, na); |
| 231 |
#endif |
| 232 |
|
| 233 |
/* |
| 234 |
* Allocate XABKEY and XABALL blocks and link them |
| 235 |
*/ |
| 236 |
|
| 237 |
xabfhc.xab$l_nxt = (char*)&xabdat; |
| 238 |
xabdat.xab$l_nxt = (char*)&xabpro; |
| 239 |
xabpro.xab$l_nxt = (char*)&xabrdt; |
| 240 |
xabrdt.xab$l_nxt = NULL; |
| 241 |
|
| 242 |
xab_chain = (xabptr)(&xabfhc); |
| 243 |
last_xab = (xabptr)(&xabrdt); |
| 244 |
|
| 245 |
#define INIT(ptr,size,type,init) \ |
| 246 |
if ( (ptr = (type *)malloc(size)) == NULL ) \ |
| 247 |
{ \ |
| 248 |
printf( "set_extra_field: Insufficient memory.\n" ); \ |
| 249 |
goto err_exit; \ |
| 250 |
} \ |
| 251 |
*(ptr) = (init); |
| 252 |
/* |
| 253 |
* Allocate and initialize all needed XABKEYs and XABALLs |
| 254 |
*/ |
| 255 |
for (i = 0; i < nk; i++) |
| 256 |
{ |
| 257 |
struct XABKEY *k; |
| 258 |
INIT(k, XKEYL, struct XABKEY, cc$rms_xabkey); |
| 259 |
k->xab$b_ref = i; |
| 260 |
if (last_xab != NULL) |
| 261 |
last_xab->xab$l_nxt = (char*)k; |
| 262 |
last_xab = (xabptr)k; |
| 263 |
} |
| 264 |
for (i = 0; i < na; i++) |
| 265 |
{ |
| 266 |
struct XABALL *a; |
| 267 |
INIT(a, XALLL, struct XABALL, cc$rms_xaball); |
| 268 |
a->xab$b_aid = i; |
| 269 |
if (last_xab != NULL) |
| 270 |
last_xab->xab$l_nxt = (char*)a; |
| 271 |
last_xab = (xabptr)a; |
| 272 |
} |
| 273 |
|
| 274 |
fab.fab$l_xab = (char*)xab_chain; |
| 275 |
#ifdef DEBUG |
| 276 |
printf("Dump of XAB chain before $DISPLAY:\n"); |
| 277 |
for (x = xab_chain; x != NULL; x = x->xab$l_nxt) |
| 278 |
dump_rms_block((uch *)x); |
| 279 |
#endif |
| 280 |
/* |
| 281 |
* Get information on the file structure etc. |
| 282 |
*/ |
| 283 |
status = sys$display(&fab, 0, 0); |
| 284 |
if (ERR(status)) |
| 285 |
{ |
| 286 |
#ifdef DEBUG |
| 287 |
printf("set_extra_field: sys$display for file %s:\n error status = %d\n", |
| 288 |
z->name, status); |
| 289 |
#endif |
| 290 |
goto err_exit; |
| 291 |
} |
| 292 |
|
| 293 |
#ifdef DEBUG |
| 294 |
printf("\nDump of XAB chain after $DISPLAY:\n"); |
| 295 |
for (x = xab_chain; x != NULL; x = x->xab$l_nxt) |
| 296 |
dump_rms_block((uch *)x); |
| 297 |
#endif |
| 298 |
|
| 299 |
fab.fab$l_xab = NULL; /* Keep XABs */ |
| 300 |
status = sys$close(&fab); |
| 301 |
if (ERR(status)) |
| 302 |
{ |
| 303 |
#ifdef DEBUG |
| 304 |
printf("set_extra_field: sys$close for file %s:\n error status = %d\n", |
| 305 |
z->name, status); |
| 306 |
#endif |
| 307 |
goto err_exit; |
| 308 |
} |
| 309 |
|
| 310 |
extra_l = EXTL0 + nk * (XKEYL + EXTHL) + na * (XALLL + EXTHL); |
| 311 |
#ifndef OLD_COMPRESS |
| 312 |
extra_l += PAD0 + (nk+na) * PAD; |
| 313 |
#endif |
| 314 |
|
| 315 |
if ( (verlen = get_vms_version(verbuf, sizeof(verbuf))) > 0 ) |
| 316 |
{ |
| 317 |
extra_l += verlen + EXTHL; |
| 318 |
#ifndef OLD_COMPRESS |
| 319 |
extra_l += PAD; |
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
if ((scan = xtra = (uch *) malloc( extra_l)) == (uch*)NULL) |
| 324 |
{ |
| 325 |
#ifdef DEBUG |
| 326 |
printf( |
| 327 |
"set_extra_field: Insufficient memory to allocate extra L buffer\n"); |
| 328 |
#endif |
| 329 |
goto err_exit; |
| 330 |
} |
| 331 |
|
| 332 |
if ((cxtra = (uch *) malloc( extra_l)) == (uch*)NULL) |
| 333 |
{ |
| 334 |
#ifdef DEBUG |
| 335 |
printf( |
| 336 |
"set_extra_field: Insufficient memory to allocate extra C buffer\n"); |
| 337 |
#endif |
| 338 |
goto err_exit; |
| 339 |
} |
| 340 |
|
| 341 |
if (verlen > 0) |
| 342 |
scan = _compress_block((struct IZ_block *)scan, (uch *)verbuf, |
| 343 |
verlen, VERSIG); |
| 344 |
|
| 345 |
/* |
| 346 |
* Zero all unusable fields to improve compression |
| 347 |
*/ |
| 348 |
fab.fab$b_fns = fab.fab$b_shr = fab.fab$b_dns = fab.fab$b_fac = 0; |
| 349 |
fab.fab$w_ifi = 0; |
| 350 |
fab.fab$l_stv = fab.fab$l_sts = fab.fab$l_ctx = 0; |
| 351 |
fab.fab$l_dna = NULL; |
| 352 |
fab.fab$l_fna = NULL; |
| 353 |
fab.fab$l_nam = NULL; |
| 354 |
#ifdef NAML$C_MAXRSS |
| 355 |
fab.fab$l_naml = NULL; |
| 356 |
#endif /* def NAML$C_MAXRSS */ |
| 357 |
fab.fab$l_xab = NULL; |
| 358 |
|
| 359 |
#ifdef DEBUG |
| 360 |
dump_rms_block( (uch *)&fab ); |
| 361 |
#endif |
| 362 |
scan = _compress_block((struct IZ_block *)scan, (uch *)&fab, FABL, FABSIG); |
| 363 |
for (x = xab_chain; x != NULL;) |
| 364 |
{ |
| 365 |
int bln; |
| 366 |
char *sig; |
| 367 |
xabptr next; |
| 368 |
|
| 369 |
next = (xabptr)(x->xab$l_nxt); |
| 370 |
x->xab$l_nxt = 0; |
| 371 |
|
| 372 |
switch (x->xab$b_cod) |
| 373 |
{ |
| 374 |
case XAB$C_ALL: |
| 375 |
bln = XALLL; |
| 376 |
sig = XALLSIG; |
| 377 |
break; |
| 378 |
case XAB$C_KEY: |
| 379 |
bln = XKEYL; |
| 380 |
sig = XKEYSIG; |
| 381 |
break; |
| 382 |
case XAB$C_PRO: |
| 383 |
bln = XPROL; |
| 384 |
sig = XPROSIG; |
| 385 |
break; |
| 386 |
case XAB$C_FHC: |
| 387 |
bln = XFHCL; |
| 388 |
sig = XFHCSIG; |
| 389 |
break; |
| 390 |
case XAB$C_DAT: |
| 391 |
bln = XDATL; |
| 392 |
sig = XDATSIG; |
| 393 |
break; |
| 394 |
case XAB$C_RDT: |
| 395 |
bln = XRDTL; |
| 396 |
sig = XRDTSIG; |
| 397 |
break; |
| 398 |
default: |
| 399 |
bln = 0; |
| 400 |
sig = 0L; |
| 401 |
break; |
| 402 |
} |
| 403 |
if (bln > 0) |
| 404 |
scan = _compress_block((struct IZ_block *)scan, (uch *)x, |
| 405 |
bln, sig); |
| 406 |
x = next; |
| 407 |
} |
| 408 |
|
| 409 |
/* Copy xtra[] data into cxtra[]. */ |
| 410 |
memcpy( cxtra, xtra, (scan- xtra)); |
| 411 |
|
| 412 |
/* Set sizes and pointers. */ |
| 413 |
z->cext = z->ext = scan- xtra; |
| 414 |
z->extra = (char*) xtra; |
| 415 |
z->cextra = (char*) cxtra; |
| 416 |
|
| 417 |
rc = RET_SUCCESS; |
| 418 |
|
| 419 |
err_exit: |
| 420 |
/* |
| 421 |
* Give up all allocated blocks |
| 422 |
*/ |
| 423 |
for (x = (struct XAB *)xab_chain; x != NULL; ) |
| 424 |
{ |
| 425 |
struct XAB *next; |
| 426 |
next = (xabptr)(x->xab$l_nxt); |
| 427 |
if (x->xab$b_cod == XAB$C_ALL || x->xab$b_cod == XAB$C_KEY) |
| 428 |
free(x); |
| 429 |
x = next; |
| 430 |
} |
| 431 |
return rc; |
| 432 |
} |
| 433 |
|
| 434 |
static int get_vms_version(verbuf, len) |
| 435 |
char *verbuf; |
| 436 |
int len; |
| 437 |
{ |
| 438 |
int i = SYI$_VERSION; |
| 439 |
int verlen = 0; |
| 440 |
struct dsc$descriptor version; |
| 441 |
char *m; |
| 442 |
|
| 443 |
version.dsc$a_pointer = verbuf; |
| 444 |
version.dsc$w_length = len - 1; |
| 445 |
version.dsc$b_dtype = DSC$K_DTYPE_B; |
| 446 |
version.dsc$b_class = DSC$K_CLASS_S; |
| 447 |
|
| 448 |
if (ERR(lib$getsyi(&i, 0, &version, &verlen, 0, 0)) || verlen == 0) |
| 449 |
return 0; |
| 450 |
|
| 451 |
/* Cut out trailing spaces "V5.4-3 " -> "V5.4-3" */ |
| 452 |
for (m = verbuf + verlen, i = verlen - 1; i > 0 && verbuf[i] == ' '; --i) |
| 453 |
--m; |
| 454 |
*m = 0; |
| 455 |
|
| 456 |
/* Cut out release number "V5.4-3" -> "V5.4" */ |
| 457 |
if ((m = strrchr(verbuf, '-')) != NULL) |
| 458 |
*m = 0; |
| 459 |
return strlen(verbuf) + 1; /* Transmit ending 0 too */ |
| 460 |
} |
| 461 |
|
| 462 |
#define CTXSIG ((ulg)('CtXx')) |
| 463 |
|
| 464 |
typedef struct user_context |
| 465 |
{ |
| 466 |
ulg sig; |
| 467 |
struct FAB *fab; |
| 468 |
struct NAM_STRUCT *nam; |
| 469 |
struct RAB *rab; |
| 470 |
uzoff_t size; |
| 471 |
uzoff_t rest; |
| 472 |
int status; |
| 473 |
} Ctx, *Ctxptr; |
| 474 |
|
| 475 |
Ctx init_ctx = |
| 476 |
{ |
| 477 |
CTXSIG, |
| 478 |
NULL, |
| 479 |
NULL, |
| 480 |
NULL, |
| 481 |
0L, |
| 482 |
0L, |
| 483 |
0 |
| 484 |
}; |
| 485 |
|
| 486 |
#define CTXL sizeof(Ctx) |
| 487 |
#define CHECK_RAB(_r) ( (_r) != NULL && \ |
| 488 |
(_r) -> rab$b_bid == RAB$C_BID && \ |
| 489 |
(_r) -> rab$b_bln == RAB$C_BLN && \ |
| 490 |
(_r) -> rab$l_ctx != 0 && \ |
| 491 |
(_r) -> rab$l_fab != NULL ) |
| 492 |
|
| 493 |
|
| 494 |
#define BLOCK_BYTES 512 |
| 495 |
|
| 496 |
/************************** |
| 497 |
* Function vms_open * |
| 498 |
**************************/ |
| 499 |
struct RAB *vms_open(name) |
| 500 |
char *name; |
| 501 |
{ |
| 502 |
struct FAB *fab; |
| 503 |
struct NAM_STRUCT *nam; |
| 504 |
struct RAB *rab; |
| 505 |
struct XABFHC *fhc; |
| 506 |
Ctxptr ctx; |
| 507 |
|
| 508 |
if ((fab = (struct FAB *) malloc(FABL)) == NULL) |
| 509 |
return NULL; |
| 510 |
|
| 511 |
if ((nam = |
| 512 |
(struct NAM_STRUCT *) malloc( sizeof( struct NAM_STRUCT))) == NULL) |
| 513 |
{ |
| 514 |
free(fab); |
| 515 |
return NULL; |
| 516 |
} |
| 517 |
|
| 518 |
if ((rab = (struct RAB *) malloc(RABL)) == NULL) |
| 519 |
{ |
| 520 |
free(fab); |
| 521 |
free(nam); |
| 522 |
return NULL; |
| 523 |
} |
| 524 |
|
| 525 |
if ((fhc = (struct XABFHC *) malloc(XFHCL)) == (struct XABFHC *)NULL) |
| 526 |
{ |
| 527 |
free(fab); |
| 528 |
free(nam); |
| 529 |
free(rab); |
| 530 |
return (struct RAB *)NULL; |
| 531 |
} |
| 532 |
if ((ctx = (Ctxptr) malloc(CTXL)) == (Ctxptr)NULL) |
| 533 |
{ |
| 534 |
free(fab); |
| 535 |
free(nam); |
| 536 |
free(rab); |
| 537 |
free(fhc); |
| 538 |
return (struct RAB *)NULL; |
| 539 |
} |
| 540 |
*fab = cc$rms_fab; |
| 541 |
*nam = CC_RMS_NAM; |
| 542 |
*rab = cc$rms_rab; |
| 543 |
*fhc = cc$rms_xabfhc; |
| 544 |
|
| 545 |
fab->FAB_NAM = nam; |
| 546 |
|
| 547 |
#ifdef NAML$C_MAXRSS |
| 548 |
|
| 549 |
fab->fab$l_dna = (char *) -1; /* Using NAML for default name. */ |
| 550 |
fab->fab$l_fna = (char *) -1; /* Using NAML for file name. */ |
| 551 |
|
| 552 |
#endif /* def NAML$C_MAXRSS */ |
| 553 |
|
| 554 |
FAB_OR_NAML( fab, nam)->FAB_OR_NAML_FNA = name; |
| 555 |
FAB_OR_NAML( fab, nam)->FAB_OR_NAML_FNS = strlen( name); |
| 556 |
|
| 557 |
fab->fab$b_fac = FAB$M_GET | FAB$M_BIO; |
| 558 |
fab->fab$l_xab = (char*)fhc; |
| 559 |
|
| 560 |
#ifdef NAML$M_OPEN_SPECIAL |
| 561 |
/* 2007-02-28 SMS. |
| 562 |
* If processing symlinks as symlinks ("-y"), then $OPEN the |
| 563 |
* link, not the target file. (Note that here the required |
| 564 |
* fab->fab$v_bio flag was set above.) |
| 565 |
*/ |
| 566 |
if (linkput) |
| 567 |
{ |
| 568 |
nam->naml$v_open_special = 1; |
| 569 |
} |
| 570 |
#endif /* def NAML$M_OPEN_SPECIAL */ |
| 571 |
|
| 572 |
if (ERR(sys$open(fab))) |
| 573 |
{ |
| 574 |
sys$close(fab); |
| 575 |
free(fab); |
| 576 |
free(nam); |
| 577 |
free(rab); |
| 578 |
free(fhc); |
| 579 |
free(ctx); |
| 580 |
return (struct RAB *)NULL; |
| 581 |
} |
| 582 |
|
| 583 |
rab->rab$l_fab = fab; |
| 584 |
rab->rab$l_rop = RAB$M_BIO; |
| 585 |
|
| 586 |
if (ERR(sys$connect(rab))) |
| 587 |
{ |
| 588 |
sys$close(fab); |
| 589 |
free(fab); |
| 590 |
free(nam); |
| 591 |
free(rab); |
| 592 |
free(ctx); |
| 593 |
return (struct RAB *)NULL; |
| 594 |
} |
| 595 |
|
| 596 |
*ctx = init_ctx; |
| 597 |
ctx->fab = fab; |
| 598 |
ctx->nam = nam; |
| 599 |
ctx->rab = rab; |
| 600 |
|
| 601 |
if (fhc->xab$l_ebk == 0) |
| 602 |
{ |
| 603 |
/* Only known size is all allocated blocks. |
| 604 |
(This occurs with a zero-length file, for example.) |
| 605 |
*/ |
| 606 |
ctx->size = |
| 607 |
ctx->rest = ((uzoff_t) fhc->xab$l_hbk)* BLOCK_BYTES; |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
/* Store normal (used) size in ->size. |
| 612 |
If only one -V, store normal (used) size in ->rest. |
| 613 |
If -VV, store allocated-blocks size in ->rest. |
| 614 |
*/ |
| 615 |
ctx->size = |
| 616 |
(((uzoff_t) fhc->xab$l_ebk)- 1)* BLOCK_BYTES+ fhc->xab$w_ffb; |
| 617 |
if (vms_native < 2) |
| 618 |
ctx->rest = ctx->size; |
| 619 |
else |
| 620 |
ctx->rest = ((uzoff_t) fhc->xab$l_hbk)* BLOCK_BYTES; |
| 621 |
} |
| 622 |
|
| 623 |
free(fhc); |
| 624 |
fab->fab$l_xab = NULL; |
| 625 |
rab->rab$l_ctx = (unsigned) ctx; |
| 626 |
return rab; |
| 627 |
} |
| 628 |
|
| 629 |
/************************** |
| 630 |
* Function vms_close * |
| 631 |
**************************/ |
| 632 |
int vms_close(rab) |
| 633 |
struct RAB *rab; |
| 634 |
{ |
| 635 |
struct FAB *fab; |
| 636 |
struct NAM_STRUCT *nam; |
| 637 |
Ctxptr ctx; |
| 638 |
|
| 639 |
if (!CHECK_RAB(rab)) |
| 640 |
return RET_ERROR; |
| 641 |
fab = (ctx = (Ctxptr)(rab->rab$l_ctx))->fab; |
| 642 |
nam = (ctx = (Ctxptr)(rab->rab$l_ctx))->nam; |
| 643 |
sys$close(fab); |
| 644 |
|
| 645 |
free(fab); |
| 646 |
free(nam); |
| 647 |
free(rab); |
| 648 |
free(ctx); |
| 649 |
|
| 650 |
return RET_SUCCESS; |
| 651 |
} |
| 652 |
|
| 653 |
/************************** |
| 654 |
* Function vms_rewind * |
| 655 |
**************************/ |
| 656 |
int vms_rewind(rab) |
| 657 |
struct RAB *rab; |
| 658 |
{ |
| 659 |
Ctxptr ctx; |
| 660 |
|
| 661 |
int status; |
| 662 |
if (!CHECK_RAB(rab)) |
| 663 |
return RET_ERROR; |
| 664 |
|
| 665 |
ctx = (Ctxptr) (rab->rab$l_ctx); |
| 666 |
if (ERR(status = sys$rewind(rab))) |
| 667 |
{ |
| 668 |
ctx->status = status; |
| 669 |
return RET_ERROR; |
| 670 |
} |
| 671 |
|
| 672 |
ctx->status = 0; |
| 673 |
ctx->rest = ctx->size; |
| 674 |
|
| 675 |
return RET_SUCCESS; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
#define KByte (2* BLOCK_BYTES) |
| 680 |
#define MAX_READ_BYTES (32* KByte) |
| 681 |
|
| 682 |
/************************** |
| 683 |
* Function vms_read * |
| 684 |
**************************/ |
| 685 |
size_t vms_read(rab, buf, size) |
| 686 |
struct RAB *rab; |
| 687 |
char *buf; |
| 688 |
size_t size; |
| 689 |
/* |
| 690 |
* size must be greater or equal to 512 ! |
| 691 |
*/ |
| 692 |
{ |
| 693 |
int status; |
| 694 |
Ctxptr ctx; |
| 695 |
|
| 696 |
ctx = (Ctxptr)rab->rab$l_ctx; |
| 697 |
|
| 698 |
if (!CHECK_RAB(rab)) |
| 699 |
return 0; |
| 700 |
|
| 701 |
if (ctx -> rest == 0) |
| 702 |
return 0; /* Eof */ |
| 703 |
|
| 704 |
/* If request is smaller than a whole block, fail. |
| 705 |
This really should never happen. (assert()?) |
| 706 |
*/ |
| 707 |
if (size < BLOCK_BYTES) |
| 708 |
return 0; |
| 709 |
|
| 710 |
/* 2004-09-27 SMS. |
| 711 |
Code here now resembles low-level QIO code in VMS_PK.C, but I |
| 712 |
doubt that sys$read() will actually get past the official EOF. |
| 713 |
*/ |
| 714 |
|
| 715 |
/* Adjust request size as appropriate. */ |
| 716 |
if (size > MAX_READ_BYTES) |
| 717 |
{ |
| 718 |
/* Restrict request to MAX_READ_BYTES. */ |
| 719 |
size = MAX_READ_BYTES; |
| 720 |
} |
| 721 |
else |
| 722 |
{ |
| 723 |
/* Round odd-ball request up to the next whole block. |
| 724 |
This really should never happen. (assert()?) |
| 725 |
*/ |
| 726 |
size = (size+ BLOCK_BYTES- 1)& ~(BLOCK_BYTES- 1); |
| 727 |
} |
| 728 |
|
| 729 |
/* Reduce "size" when next (last) read would overrun the EOF, |
| 730 |
but never below one byte (so we'll always get a nice EOF). |
| 731 |
*/ |
| 732 |
if (size > ctx->rest) |
| 733 |
size = ctx->rest; |
| 734 |
if (size == 0) |
| 735 |
size = 1; |
| 736 |
|
| 737 |
rab->rab$l_ubf = buf; |
| 738 |
rab->rab$w_usz = size; |
| 739 |
status = sys$read(rab); |
| 740 |
|
| 741 |
if (!ERR(status) && rab->rab$w_rsz > 0) |
| 742 |
{ |
| 743 |
ctx -> status = 0; |
| 744 |
ctx -> rest -= rab->rab$w_rsz; |
| 745 |
return rab->rab$w_rsz; |
| 746 |
} |
| 747 |
else |
| 748 |
{ |
| 749 |
ctx->status = (status==RMS$_EOF ? 0:status); |
| 750 |
if (status == RMS$_EOF) |
| 751 |
ctx -> rest = 0; |
| 752 |
return 0; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
/************************** |
| 757 |
* Function vms_error * |
| 758 |
**************************/ |
| 759 |
int vms_error(rab) |
| 760 |
struct RAB *rab; |
| 761 |
{ |
| 762 |
if (!CHECK_RAB(rab)) |
| 763 |
return RET_ERROR; |
| 764 |
return ((Ctxptr) (rab->rab$l_ctx))->status; |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
#ifdef DEBUG |
| 769 |
static void dump_rms_block(p) |
| 770 |
uch *p; |
| 771 |
{ |
| 772 |
uch bid, len; |
| 773 |
int err; |
| 774 |
char *type; |
| 775 |
char buf[132]; |
| 776 |
int i; |
| 777 |
|
| 778 |
err = 0; |
| 779 |
bid = p[0]; |
| 780 |
len = p[1]; |
| 781 |
switch (bid) |
| 782 |
{ |
| 783 |
case FAB$C_BID: |
| 784 |
type = "FAB"; |
| 785 |
break; |
| 786 |
case XAB$C_ALL: |
| 787 |
type = "xabALL"; |
| 788 |
break; |
| 789 |
case XAB$C_KEY: |
| 790 |
type = "xabKEY"; |
| 791 |
break; |
| 792 |
case XAB$C_DAT: |
| 793 |
type = "xabDAT"; |
| 794 |
break; |
| 795 |
case XAB$C_RDT: |
| 796 |
type = "xabRDT"; |
| 797 |
break; |
| 798 |
case XAB$C_FHC: |
| 799 |
type = "xabFHC"; |
| 800 |
break; |
| 801 |
case XAB$C_PRO: |
| 802 |
type = "xabPRO"; |
| 803 |
break; |
| 804 |
default: |
| 805 |
type = "Unknown"; |
| 806 |
err = 1; |
| 807 |
break; |
| 808 |
} |
| 809 |
printf("Block @%08X of type %s (%d).", p, type, bid); |
| 810 |
if (err) |
| 811 |
{ |
| 812 |
printf("\n"); |
| 813 |
return; |
| 814 |
} |
| 815 |
printf(" Size = %d\n", len); |
| 816 |
printf(" Offset - Hex - Dec\n"); |
| 817 |
for (i = 0; i < len; i += 8) |
| 818 |
{ |
| 819 |
int j; |
| 820 |
|
| 821 |
printf("%3d - ", i); |
| 822 |
for (j = 0; j < 8; j++) |
| 823 |
if (i + j < len) |
| 824 |
printf("%02X ", p[i + j]); |
| 825 |
else |
| 826 |
printf(" "); |
| 827 |
printf(" - "); |
| 828 |
for (j = 0; j < 8; j++) |
| 829 |
if (i + j < len) |
| 830 |
printf("%03d ", p[i + j]); |
| 831 |
else |
| 832 |
printf(" "); |
| 833 |
printf("\n"); |
| 834 |
} |
| 835 |
} |
| 836 |
#endif /* DEBUG */ |
| 837 |
|
| 838 |
#ifdef OLD_COMPRESS |
| 839 |
# define BC_METHOD EB_IZVMS_BC00 |
| 840 |
# define COMP_BLK(to,tos,from,froms) _compress( from,to,froms ) |
| 841 |
#else |
| 842 |
# define BC_METHOD EB_IZVMS_BCDEFL |
| 843 |
# define COMP_BLK(to,tos,from,froms) memcompress(to,tos,from,froms) |
| 844 |
#endif |
| 845 |
|
| 846 |
static uch *_compress_block(to,from,size,sig) |
| 847 |
register struct IZ_block *to; |
| 848 |
uch *from; |
| 849 |
int size; |
| 850 |
char *sig; |
| 851 |
{ |
| 852 |
ulg cl; |
| 853 |
to -> sig = *(ush*)IZ_SIGNATURE; |
| 854 |
to -> bid = *(ulg*)(sig); |
| 855 |
to -> flags = BC_METHOD; |
| 856 |
to -> length = size; |
| 857 |
#ifdef DEBUG |
| 858 |
printf("\nmemcompr(%d,%d,%d,%d)\n",&(to->body[0]),size+PAD,from,size); |
| 859 |
#endif |
| 860 |
cl = COMP_BLK( &(to->body[0]), size+PAD, from, size ); |
| 861 |
#ifdef DEBUG |
| 862 |
printf("Compressed to %d\n",cl); |
| 863 |
#endif |
| 864 |
if (cl >= size) |
| 865 |
{ |
| 866 |
memcpy(&(to->body[0]), from, size); |
| 867 |
to->flags = EB_IZVMS_BCSTOR; |
| 868 |
cl = size; |
| 869 |
#ifdef DEBUG |
| 870 |
printf("Storing block...\n"); |
| 871 |
#endif |
| 872 |
} |
| 873 |
return (uch*)(to) + (to->size = cl + EXTBSL + RESL) + EB_HEADSIZE; |
| 874 |
} |
| 875 |
|
| 876 |
#define NBITS 32 |
| 877 |
|
| 878 |
static int _compress(from,to,size) |
| 879 |
uch *from,*to; |
| 880 |
int size; |
| 881 |
{ |
| 882 |
int off=0; |
| 883 |
ulg bitbuf=0; |
| 884 |
int bitcnt=0; |
| 885 |
int i; |
| 886 |
|
| 887 |
#define _BIT(val,len) { \ |
| 888 |
if (bitcnt + (len) > NBITS) \ |
| 889 |
while(bitcnt >= 8) \ |
| 890 |
{ \ |
| 891 |
to[off++] = (uch)bitbuf; \ |
| 892 |
bitbuf >>= 8; \ |
| 893 |
bitcnt -= 8; \ |
| 894 |
} \ |
| 895 |
bitbuf |= ((ulg)(val))<<bitcnt; \ |
| 896 |
bitcnt += len; \ |
| 897 |
} |
| 898 |
|
| 899 |
#define _FLUSH { \ |
| 900 |
while(bitcnt>0) \ |
| 901 |
{ \ |
| 902 |
to[off++] = (uch)bitbuf; \ |
| 903 |
bitbuf >>= 8; \ |
| 904 |
bitcnt -= 8; \ |
| 905 |
} \ |
| 906 |
} |
| 907 |
|
| 908 |
for (i=0; i<size; i++) |
| 909 |
{ |
| 910 |
if (from[i]) |
| 911 |
{ |
| 912 |
_BIT(1,1); |
| 913 |
_BIT(from[i],8); |
| 914 |
} |
| 915 |
else |
| 916 |
_BIT(0,1); |
| 917 |
} |
| 918 |
_FLUSH; |
| 919 |
return off; |
| 920 |
} |
| 921 |
|
| 922 |
#endif /* !UTIL */ |
| 923 |
|
| 924 |
#endif /* ndef VMS_PK_EXTRA */ |
| 925 |
|
| 926 |
#endif /* VMS */ |