1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright(c) 1988 AT&T 24*0Sstevel@tonic-gate * All Rights Reserved 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate */ 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate /* 29*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 30*0Sstevel@tonic-gate * Use is subject to license terms. 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include "mcs.h" 36*0Sstevel@tonic-gate #include "extern.h" 37*0Sstevel@tonic-gate #include "gelf.h" 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* 40*0Sstevel@tonic-gate * Function prototypes. 41*0Sstevel@tonic-gate */ 42*0Sstevel@tonic-gate static void docompress(section_info_table *); 43*0Sstevel@tonic-gate static char *compress(char *, size_t *); 44*0Sstevel@tonic-gate static void doappend(char *, section_info_table *); 45*0Sstevel@tonic-gate static void doprint(char *, section_info_table *); 46*0Sstevel@tonic-gate static void dozap(section_info_table *); 47*0Sstevel@tonic-gate static int dohash(char *); 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * Apply the actions specified by the user. 53*0Sstevel@tonic-gate */ 54*0Sstevel@tonic-gate int 55*0Sstevel@tonic-gate apply_action(section_info_table *info, char *cur_file, Cmd_Info *cmd_info) 56*0Sstevel@tonic-gate { 57*0Sstevel@tonic-gate int act_index; 58*0Sstevel@tonic-gate int ret = 0; 59*0Sstevel@tonic-gate GElf_Shdr shdr; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate (void) gelf_getshdr(info->scn, &shdr); 62*0Sstevel@tonic-gate for (act_index = 0; act_index < actmax; act_index++) { 63*0Sstevel@tonic-gate Action[act_index].a_cnt++; 64*0Sstevel@tonic-gate switch (Action[act_index].a_action) { 65*0Sstevel@tonic-gate case ACT_ZAP: 66*0Sstevel@tonic-gate if (GET_ACTION(info->flags) == ACT_DELETE) 67*0Sstevel@tonic-gate break; 68*0Sstevel@tonic-gate dozap(info); 69*0Sstevel@tonic-gate SET_ACTION(info->flags, ACT_ZAP); 70*0Sstevel@tonic-gate SET_MODIFIED(info->flags); 71*0Sstevel@tonic-gate break; 72*0Sstevel@tonic-gate case ACT_PRINT: 73*0Sstevel@tonic-gate if (GET_ACTION(info->flags) == ACT_DELETE) 74*0Sstevel@tonic-gate break; 75*0Sstevel@tonic-gate if (shdr.sh_type == SHT_NOBITS) { 76*0Sstevel@tonic-gate error_message(ACT_PRINT_ERROR, 77*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 78*0Sstevel@tonic-gate prog, cur_file, SECT_NAME); 79*0Sstevel@tonic-gate break; 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate doprint(cur_file, info); 82*0Sstevel@tonic-gate break; 83*0Sstevel@tonic-gate case ACT_DELETE: 84*0Sstevel@tonic-gate /* 85*0Sstevel@tonic-gate * If I am strip command, this is the 86*0Sstevel@tonic-gate * only action I can take. 87*0Sstevel@tonic-gate */ 88*0Sstevel@tonic-gate if (GET_ACTION(info->flags) == ACT_DELETE) 89*0Sstevel@tonic-gate break; 90*0Sstevel@tonic-gate if (GET_LOC(info->flags) == IN) { 91*0Sstevel@tonic-gate /* 92*0Sstevel@tonic-gate * If I am 'strip', I have to 93*0Sstevel@tonic-gate * unset the candidate flag and 94*0Sstevel@tonic-gate * unset the error return code. 95*0Sstevel@tonic-gate */ 96*0Sstevel@tonic-gate if (CHK_OPT(info, I_AM_STRIP)) { 97*0Sstevel@tonic-gate ret = 0; 98*0Sstevel@tonic-gate UNSET_CANDIDATE(info->flags); 99*0Sstevel@tonic-gate } else { 100*0Sstevel@tonic-gate ret++; 101*0Sstevel@tonic-gate error_message(ACT_DELETE1_ERROR, 102*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 103*0Sstevel@tonic-gate prog, cur_file, info->name); 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate break; 106*0Sstevel@tonic-gate } else if (info->rel_loc == IN) { 107*0Sstevel@tonic-gate /* 108*0Sstevel@tonic-gate * If I am 'strip', I have to 109*0Sstevel@tonic-gate * unset the candidate flag and 110*0Sstevel@tonic-gate * unset the error return code. 111*0Sstevel@tonic-gate */ 112*0Sstevel@tonic-gate if (CHK_OPT(info, I_AM_STRIP)) { 113*0Sstevel@tonic-gate ret = 0; 114*0Sstevel@tonic-gate UNSET_CANDIDATE(info->flags); 115*0Sstevel@tonic-gate } else { 116*0Sstevel@tonic-gate ret++; 117*0Sstevel@tonic-gate error_message(ACT_DELETE2_ERROR, 118*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 119*0Sstevel@tonic-gate prog, cur_file, SECT_NAME, 120*0Sstevel@tonic-gate info->rel_name); 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate break; 123*0Sstevel@tonic-gate } else if (GET_LOC(info->flags) == PRIOR) { 124*0Sstevel@tonic-gate /* 125*0Sstevel@tonic-gate * I can not delete this 126*0Sstevel@tonic-gate * section. I can only NULL 127*0Sstevel@tonic-gate * this out. 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate info->secno = (GElf_Word)NULLED; 130*0Sstevel@tonic-gate (cmd_info->no_of_nulled)++; 131*0Sstevel@tonic-gate } else { 132*0Sstevel@tonic-gate info->secno = (GElf_Word)DELETED; 133*0Sstevel@tonic-gate (cmd_info->no_of_delete)++; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate SET_ACTION(info->flags, ACT_DELETE); 136*0Sstevel@tonic-gate SET_MODIFIED(info->flags); 137*0Sstevel@tonic-gate break; 138*0Sstevel@tonic-gate case ACT_APPEND: 139*0Sstevel@tonic-gate if (shdr.sh_type == SHT_NOBITS) { 140*0Sstevel@tonic-gate ret++; 141*0Sstevel@tonic-gate error_message(ACT_APPEND1_ERROR, 142*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 143*0Sstevel@tonic-gate prog, cur_file, SECT_NAME); 144*0Sstevel@tonic-gate break; 145*0Sstevel@tonic-gate } else if (GET_LOC(info->flags) == IN) { 146*0Sstevel@tonic-gate ret++; 147*0Sstevel@tonic-gate error_message(ACT_APPEND2_ERROR, 148*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 149*0Sstevel@tonic-gate prog, cur_file, SECT_NAME); 150*0Sstevel@tonic-gate break; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate doappend(Action[act_index].a_string, info); 153*0Sstevel@tonic-gate (cmd_info->no_of_append)++; 154*0Sstevel@tonic-gate info->secno = info->osecno; 155*0Sstevel@tonic-gate SET_ACTION(info->flags, ACT_APPEND); 156*0Sstevel@tonic-gate SET_MODIFIED(info->flags); 157*0Sstevel@tonic-gate if (GET_LOC(info->flags) == PRIOR) 158*0Sstevel@tonic-gate info->secno = (GElf_Word)EXPANDED; 159*0Sstevel@tonic-gate break; 160*0Sstevel@tonic-gate case ACT_COMPRESS: 161*0Sstevel@tonic-gate /* 162*0Sstevel@tonic-gate * If this section is already deleted, 163*0Sstevel@tonic-gate * don't do anything. 164*0Sstevel@tonic-gate */ 165*0Sstevel@tonic-gate if (GET_ACTION(info->flags) == ACT_DELETE) 166*0Sstevel@tonic-gate break; 167*0Sstevel@tonic-gate if (shdr.sh_type == SHT_NOBITS) { 168*0Sstevel@tonic-gate ret++; 169*0Sstevel@tonic-gate error_message(ACT_COMPRESS1_ERROR, 170*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 171*0Sstevel@tonic-gate prog, cur_file, SECT_NAME); 172*0Sstevel@tonic-gate break; 173*0Sstevel@tonic-gate } else if (GET_LOC(info->flags) == IN) { 174*0Sstevel@tonic-gate ret++; 175*0Sstevel@tonic-gate error_message(ACT_COMPRESS2_ERROR, 176*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 177*0Sstevel@tonic-gate prog, cur_file, SECT_NAME); 178*0Sstevel@tonic-gate break; 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate docompress(info); 182*0Sstevel@tonic-gate (cmd_info->no_of_compressed)++; 183*0Sstevel@tonic-gate SET_ACTION(info->flags, ACT_COMPRESS); 184*0Sstevel@tonic-gate SET_MODIFIED(info->flags); 185*0Sstevel@tonic-gate if (GET_LOC(info->flags) == PRIOR) 186*0Sstevel@tonic-gate info->secno = (GElf_Word)SHRUNK; 187*0Sstevel@tonic-gate break; 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate return (ret); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate /* 194*0Sstevel@tonic-gate * ACT_ZAP 195*0Sstevel@tonic-gate */ 196*0Sstevel@tonic-gate static void 197*0Sstevel@tonic-gate dozap(section_info_table *info) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate Elf_Data *data; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate info->mdata = data = malloc(sizeof (Elf_Data)); 202*0Sstevel@tonic-gate if (data == NULL) { 203*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 204*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 205*0Sstevel@tonic-gate prog); 206*0Sstevel@tonic-gate exit(1); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate *data = *info->data; 209*0Sstevel@tonic-gate data->d_buf = calloc(1, data->d_size); 210*0Sstevel@tonic-gate if (data->d_buf == NULL) { 211*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 212*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 213*0Sstevel@tonic-gate prog); 214*0Sstevel@tonic-gate exit(1); 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate /* 219*0Sstevel@tonic-gate * ACT_PRINT 220*0Sstevel@tonic-gate */ 221*0Sstevel@tonic-gate static void 222*0Sstevel@tonic-gate doprint(char *cur_file, section_info_table *info) 223*0Sstevel@tonic-gate { 224*0Sstevel@tonic-gate Elf_Data *data; 225*0Sstevel@tonic-gate size_t temp_size; 226*0Sstevel@tonic-gate char *temp_string; 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate if (GET_MODIFIED(info->flags) == 0) 229*0Sstevel@tonic-gate data = info->data; 230*0Sstevel@tonic-gate else 231*0Sstevel@tonic-gate data = info->mdata; 232*0Sstevel@tonic-gate if (data == 0) 233*0Sstevel@tonic-gate return; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate temp_size = data->d_size; 236*0Sstevel@tonic-gate temp_string = data->d_buf; 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate if (temp_size == 0) 239*0Sstevel@tonic-gate return; 240*0Sstevel@tonic-gate (void) fprintf(stdout, "%s:\n", cur_file); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate while (temp_size--) { 243*0Sstevel@tonic-gate char c = *temp_string++; 244*0Sstevel@tonic-gate switch (c) { 245*0Sstevel@tonic-gate case '\0': 246*0Sstevel@tonic-gate (void) putchar('\n'); 247*0Sstevel@tonic-gate break; 248*0Sstevel@tonic-gate default: 249*0Sstevel@tonic-gate (void) putchar(c); 250*0Sstevel@tonic-gate break; 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate (void) putchar('\n'); 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate /* 257*0Sstevel@tonic-gate * ACT_APPEND 258*0Sstevel@tonic-gate */ 259*0Sstevel@tonic-gate static void 260*0Sstevel@tonic-gate doappend(char *a_string, section_info_table *info) 261*0Sstevel@tonic-gate { 262*0Sstevel@tonic-gate Elf_Data *data; 263*0Sstevel@tonic-gate char *p; 264*0Sstevel@tonic-gate size_t len; 265*0Sstevel@tonic-gate char *tp; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate /* 268*0Sstevel@tonic-gate * Get the length of the string to be added. 269*0Sstevel@tonic-gate */ 270*0Sstevel@tonic-gate len = strlen(a_string); 271*0Sstevel@tonic-gate if (len == 0) 272*0Sstevel@tonic-gate return; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate /* 275*0Sstevel@tonic-gate * Every modification operation will be done 276*0Sstevel@tonic-gate * to a new Elf_Data descriptor. 277*0Sstevel@tonic-gate */ 278*0Sstevel@tonic-gate if (info->mdata == 0) { 279*0Sstevel@tonic-gate /* 280*0Sstevel@tonic-gate * mdata is not allocated yet. 281*0Sstevel@tonic-gate * Allocate the data and set it. 282*0Sstevel@tonic-gate */ 283*0Sstevel@tonic-gate info->mdata = data = calloc(1, sizeof (Elf_Data)); 284*0Sstevel@tonic-gate if (data == NULL) { 285*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 286*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 287*0Sstevel@tonic-gate prog); 288*0Sstevel@tonic-gate exit(1); 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate *data = *info->data; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate /* 293*0Sstevel@tonic-gate * Check if the section is deleted or not. 294*0Sstevel@tonic-gate * Or if the size is 0 or not. 295*0Sstevel@tonic-gate */ 296*0Sstevel@tonic-gate if ((GET_ACTION(info->flags) == ACT_DELETE) || 297*0Sstevel@tonic-gate data->d_size == 0) { 298*0Sstevel@tonic-gate /* 299*0Sstevel@tonic-gate * The section was deleated. 300*0Sstevel@tonic-gate * But now, the user wants to add data to this 301*0Sstevel@tonic-gate * section. 302*0Sstevel@tonic-gate */ 303*0Sstevel@tonic-gate data->d_buf = calloc(1, len + 2); 304*0Sstevel@tonic-gate if (data->d_buf == 0) { 305*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 306*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 307*0Sstevel@tonic-gate prog); 308*0Sstevel@tonic-gate exit(1); 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate tp = (char *)data->d_buf; 311*0Sstevel@tonic-gate (void) memcpy(& tp[1], a_string, len + 1); 312*0Sstevel@tonic-gate data->d_size = len + 2; 313*0Sstevel@tonic-gate } else { 314*0Sstevel@tonic-gate /* 315*0Sstevel@tonic-gate * The user wants to add data to the section. 316*0Sstevel@tonic-gate * I am not going to change the original data. 317*0Sstevel@tonic-gate * Do the modification on the new one. 318*0Sstevel@tonic-gate */ 319*0Sstevel@tonic-gate p = malloc(len + 1 + data->d_size); 320*0Sstevel@tonic-gate if (p == NULL) { 321*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 322*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 323*0Sstevel@tonic-gate prog); 324*0Sstevel@tonic-gate exit(1); 325*0Sstevel@tonic-gate } 326*0Sstevel@tonic-gate (void) memcpy(p, data->d_buf, data->d_size); 327*0Sstevel@tonic-gate (void) memcpy(&p[data->d_size], a_string, len + 1); 328*0Sstevel@tonic-gate data->d_buf = p; 329*0Sstevel@tonic-gate data->d_size = data->d_size + len + 1; 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate } else { 332*0Sstevel@tonic-gate /* 333*0Sstevel@tonic-gate * mdata is already allocated. 334*0Sstevel@tonic-gate * Modify it. 335*0Sstevel@tonic-gate */ 336*0Sstevel@tonic-gate data = info->mdata; 337*0Sstevel@tonic-gate if ((GET_ACTION(info->flags) == ACT_DELETE) || 338*0Sstevel@tonic-gate data->d_size == 0) { 339*0Sstevel@tonic-gate /* 340*0Sstevel@tonic-gate * The section was deleated. 341*0Sstevel@tonic-gate * But now, the user wants to add data to this 342*0Sstevel@tonic-gate * section. 343*0Sstevel@tonic-gate */ 344*0Sstevel@tonic-gate if (data->d_buf) 345*0Sstevel@tonic-gate free(data->d_buf); 346*0Sstevel@tonic-gate data->d_buf = calloc(1, len + 2); 347*0Sstevel@tonic-gate if (data->d_buf == 0) { 348*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 349*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 350*0Sstevel@tonic-gate prog); 351*0Sstevel@tonic-gate exit(1); 352*0Sstevel@tonic-gate } 353*0Sstevel@tonic-gate tp = (char *)data->d_buf; 354*0Sstevel@tonic-gate (void) memcpy(&tp[1], a_string, len + 1); 355*0Sstevel@tonic-gate data->d_size = len + 2; 356*0Sstevel@tonic-gate } else { 357*0Sstevel@tonic-gate /* 358*0Sstevel@tonic-gate * The user wants to add data to the section. 359*0Sstevel@tonic-gate * I am not going to change the original data. 360*0Sstevel@tonic-gate * Do the modification on the new one. 361*0Sstevel@tonic-gate */ 362*0Sstevel@tonic-gate p = malloc(len + 1 + data->d_size); 363*0Sstevel@tonic-gate if (p == NULL) { 364*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 365*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 366*0Sstevel@tonic-gate prog); 367*0Sstevel@tonic-gate exit(1); 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate (void) memcpy(p, data->d_buf, data->d_size); 370*0Sstevel@tonic-gate (void) memcpy(&p[data->d_size], a_string, len + 1); 371*0Sstevel@tonic-gate free(data->d_buf); 372*0Sstevel@tonic-gate data->d_buf = p; 373*0Sstevel@tonic-gate data->d_size = data->d_size + len + 1; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gate /* 379*0Sstevel@tonic-gate * ACT_COMPRESS 380*0Sstevel@tonic-gate */ 381*0Sstevel@tonic-gate #define HALFLONG 16 382*0Sstevel@tonic-gate #define low(x) (x&((1L<<HALFLONG)-1)) 383*0Sstevel@tonic-gate #define high(x) (x>>HALFLONG) 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate static void 386*0Sstevel@tonic-gate docompress(section_info_table *info) 387*0Sstevel@tonic-gate { 388*0Sstevel@tonic-gate Elf_Data *data; 389*0Sstevel@tonic-gate size_t size; 390*0Sstevel@tonic-gate char *buf; 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate if (info->mdata == 0) { 393*0Sstevel@tonic-gate /* 394*0Sstevel@tonic-gate * mdata is not allocated yet. 395*0Sstevel@tonic-gate * Allocate the data and set it. 396*0Sstevel@tonic-gate */ 397*0Sstevel@tonic-gate char *p; 398*0Sstevel@tonic-gate info->mdata = data = calloc(1, sizeof (Elf_Data)); 399*0Sstevel@tonic-gate if (data == NULL) { 400*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 401*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 402*0Sstevel@tonic-gate prog); 403*0Sstevel@tonic-gate exit(1); 404*0Sstevel@tonic-gate } 405*0Sstevel@tonic-gate *data = *info->data; 406*0Sstevel@tonic-gate p = malloc(data->d_size); 407*0Sstevel@tonic-gate (void) memcpy(p, (char *)data->d_buf, data->d_size); 408*0Sstevel@tonic-gate data->d_buf = p; 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate size = info->mdata->d_size; 411*0Sstevel@tonic-gate buf = (char *)info->mdata->d_buf; 412*0Sstevel@tonic-gate buf = compress(buf, &size); 413*0Sstevel@tonic-gate info->mdata->d_buf = buf; 414*0Sstevel@tonic-gate info->mdata->d_size = size; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate static char * 418*0Sstevel@tonic-gate compress(char *str, size_t *size) 419*0Sstevel@tonic-gate { 420*0Sstevel@tonic-gate int hash; 421*0Sstevel@tonic-gate int i; 422*0Sstevel@tonic-gate size_t temp_string_size = 0; 423*0Sstevel@tonic-gate size_t o_size = *size; 424*0Sstevel@tonic-gate char *temp_string = str; 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate int *hash_key; 427*0Sstevel@tonic-gate size_t hash_num; 428*0Sstevel@tonic-gate size_t hash_end; 429*0Sstevel@tonic-gate size_t *hash_str; 430*0Sstevel@tonic-gate char *strings; 431*0Sstevel@tonic-gate size_t next_str; 432*0Sstevel@tonic-gate size_t str_size; 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate hash_key = malloc(sizeof (int) * 200); 435*0Sstevel@tonic-gate hash_end = 200; 436*0Sstevel@tonic-gate hash_str = malloc(sizeof (size_t) * 200); 437*0Sstevel@tonic-gate str_size = o_size+1; 438*0Sstevel@tonic-gate strings = malloc(str_size); 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate if (hash_key == NULL || hash_str == NULL || strings == NULL) { 441*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 442*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 443*0Sstevel@tonic-gate prog); 444*0Sstevel@tonic-gate mcs_exit(FAILURE); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate hash_num = 0; 448*0Sstevel@tonic-gate next_str = 0; 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate while (temp_string_size < o_size) { 451*0Sstevel@tonic-gate size_t pos; 452*0Sstevel@tonic-gate char c; 453*0Sstevel@tonic-gate /* 454*0Sstevel@tonic-gate * Get a string 455*0Sstevel@tonic-gate */ 456*0Sstevel@tonic-gate pos = next_str; 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate while ((c = *(temp_string++)) != '\0' && 459*0Sstevel@tonic-gate (temp_string_size + (next_str - pos)) <= o_size) { 460*0Sstevel@tonic-gate if (next_str >= str_size) { 461*0Sstevel@tonic-gate str_size *= 2; 462*0Sstevel@tonic-gate if ((strings = (char *) 463*0Sstevel@tonic-gate realloc(strings, str_size)) == NULL) { 464*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 465*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 466*0Sstevel@tonic-gate prog); 467*0Sstevel@tonic-gate mcs_exit(FAILURE); 468*0Sstevel@tonic-gate } 469*0Sstevel@tonic-gate } 470*0Sstevel@tonic-gate strings[next_str++] = c; 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate if (next_str >= str_size) { 474*0Sstevel@tonic-gate str_size *= 2; 475*0Sstevel@tonic-gate if ((strings = (char *) 476*0Sstevel@tonic-gate realloc(strings, str_size)) == NULL) { 477*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 478*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 479*0Sstevel@tonic-gate prog); 480*0Sstevel@tonic-gate mcs_exit(FAILURE); 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate } 483*0Sstevel@tonic-gate strings[next_str++] = NULL; 484*0Sstevel@tonic-gate /* 485*0Sstevel@tonic-gate * End get string 486*0Sstevel@tonic-gate */ 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate temp_string_size += (next_str - pos); 489*0Sstevel@tonic-gate hash = dohash(pos + strings); 490*0Sstevel@tonic-gate for (i = 0; i < hash_num; i++) { 491*0Sstevel@tonic-gate if (hash != hash_key[i]) 492*0Sstevel@tonic-gate continue; 493*0Sstevel@tonic-gate if (strcmp(pos + strings, hash_str[i] + strings) == 0) 494*0Sstevel@tonic-gate break; 495*0Sstevel@tonic-gate } 496*0Sstevel@tonic-gate if (i != hash_num) { 497*0Sstevel@tonic-gate next_str = pos; 498*0Sstevel@tonic-gate continue; 499*0Sstevel@tonic-gate } 500*0Sstevel@tonic-gate if (hash_num == hash_end) { 501*0Sstevel@tonic-gate hash_end *= 2; 502*0Sstevel@tonic-gate hash_key = realloc((char *)hash_key, 503*0Sstevel@tonic-gate hash_end * sizeof (int)); 504*0Sstevel@tonic-gate hash_str = realloc((char *)hash_str, 505*0Sstevel@tonic-gate hash_end * sizeof (size_t)); 506*0Sstevel@tonic-gate if (hash_key == NULL || hash_str == NULL) { 507*0Sstevel@tonic-gate error_message(MALLOC_ERROR, 508*0Sstevel@tonic-gate PLAIN_ERROR, (char *)0, 509*0Sstevel@tonic-gate prog); 510*0Sstevel@tonic-gate mcs_exit(FAILURE); 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate hash_key[hash_num] = hash; 514*0Sstevel@tonic-gate hash_str[hash_num++] = pos; 515*0Sstevel@tonic-gate } 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate /* 518*0Sstevel@tonic-gate * Clean up 519*0Sstevel@tonic-gate */ 520*0Sstevel@tonic-gate free(hash_key); 521*0Sstevel@tonic-gate free(hash_str); 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate /* 524*0Sstevel@tonic-gate * Return 525*0Sstevel@tonic-gate */ 526*0Sstevel@tonic-gate if (next_str != o_size) { 527*0Sstevel@tonic-gate /* 528*0Sstevel@tonic-gate * string compressed. 529*0Sstevel@tonic-gate */ 530*0Sstevel@tonic-gate *size = next_str; 531*0Sstevel@tonic-gate free(str); 532*0Sstevel@tonic-gate str = malloc(next_str); 533*0Sstevel@tonic-gate (void) memcpy(str, strings, next_str); 534*0Sstevel@tonic-gate } 535*0Sstevel@tonic-gate free(strings); 536*0Sstevel@tonic-gate return (str); 537*0Sstevel@tonic-gate } 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate static int 540*0Sstevel@tonic-gate dohash(char *str) 541*0Sstevel@tonic-gate { 542*0Sstevel@tonic-gate long sum; 543*0Sstevel@tonic-gate unsigned shift; 544*0Sstevel@tonic-gate int t; 545*0Sstevel@tonic-gate sum = 1; 546*0Sstevel@tonic-gate for (shift = 0; (t = *str++) != NULL; shift += 7) { 547*0Sstevel@tonic-gate sum += (long)t << (shift %= HALFLONG); 548*0Sstevel@tonic-gate } 549*0Sstevel@tonic-gate sum = low(sum) + high(sum); 550*0Sstevel@tonic-gate /* LINTED */ 551*0Sstevel@tonic-gate return ((short)low(sum) + (short)high(sum)); 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate /* 555*0Sstevel@tonic-gate * Append an item to the specified list, and return a pointer to the list 556*0Sstevel@tonic-gate * node created. 557*0Sstevel@tonic-gate */ 558*0Sstevel@tonic-gate Listnode * 559*0Sstevel@tonic-gate list_appendc(List *lst, const void *item) 560*0Sstevel@tonic-gate { 561*0Sstevel@tonic-gate Listnode *_lnp; 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0) 564*0Sstevel@tonic-gate return (0); 565*0Sstevel@tonic-gate 566*0Sstevel@tonic-gate _lnp->data = (void *)item; 567*0Sstevel@tonic-gate _lnp->next = NULL; 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (lst->head == NULL) 570*0Sstevel@tonic-gate lst->tail = lst->head = _lnp; 571*0Sstevel@tonic-gate else { 572*0Sstevel@tonic-gate lst->tail->next = _lnp; 573*0Sstevel@tonic-gate lst->tail = lst->tail->next; 574*0Sstevel@tonic-gate } 575*0Sstevel@tonic-gate return (_lnp); 576*0Sstevel@tonic-gate } 577