dump.c revision 2c28215423293e443469a07ae7011135d058b671
1/* 2* Disktest 3* Copyright (c) International Business Machines Corp., 2001 4* 5* 6* This program is free software; you can redistribute it and/or modify 7* it under the terms of the GNU General Public License as published by 8* the Free Software Foundation; either version 2 of the License, or 9* (at your option) any later version. 10* 11* This program is distributed in the hope that it will be useful, 12* but WITHOUT ANY WARRANTY; without even the implied warranty of 13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14* GNU General Public License for more details. 15* 16* You should have received a copy of the GNU General Public License 17* along with this program; if not, write to the Free Software 18* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19* 20* Please send e-mail to yardleyb@us.ibm.com if you have 21* questions or comments. 22* 23* Project Website: TBD 24* 25* $Id: dump.c,v 1.7 2009/02/26 12:02:22 subrata_modak Exp $ 26* 27*/ 28#include <stdio.h> /* *printf() */ 29#include <string.h> /* memset(), strn***() */ 30#include <ctype.h> /* isprint() */ 31#include <stdlib.h> /* malloc(), free() */ 32 33#include "defs.h" 34#include "io.h" 35#include "sfunc.h" 36#include "dump.h" 37 38int format_str(size_t iBytes, const char *ibuff, size_t ibuff_siz, char *obuff, size_t obuff_siz) 39{ 40 unsigned int i; 41 char buff[10]; 42 static size_t TotalBytes = 0; 43 44 if ((iBytes == 0) && 45 (ibuff == NULL) && (ibuff_siz == 0) && 46 (obuff == NULL) && (obuff_siz == 0)) { 47 TotalBytes = 0; 48 return 0; 49 } 50 51 if ((ibuff == NULL) || (obuff == NULL) || (iBytes < 1)) return -1; 52 53 memset(obuff, 0, obuff_siz); 54 sprintf(buff,"%08lX", (long)TotalBytes); 55 strncat(obuff, buff, (obuff_siz-1)-strlen(obuff)); 56 for (i=0;i<iBytes;i++) { 57 if ((i%4) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 58 if ((i%8) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 59 sprintf(buff,"%02X ", *(ibuff+i)); 60 strncat(obuff, buff, (obuff_siz-1)-strlen(obuff)); 61 } 62 for (;i<ibuff_siz;i++) { 63 if ((i%4) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 64 if ((i%8) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 65 strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 66 } 67 strncat(obuff, " ", (obuff_siz-1)-strlen(obuff)); 68 for (i=0;i<iBytes;i++) { 69 sprintf(buff, "%c", (isprint(*(ibuff+i))) ? *(ibuff+i) : '.'); 70 strncat(obuff, buff, (obuff_siz-1)-strlen(obuff)); 71 } 72 TotalBytes += iBytes; 73 return 0; 74} 75 76int format_raw(size_t iBytes, const char *ibuff, char *obuff, size_t obuff_siz) 77{ 78 unsigned int i; 79 char buff[10]; 80 static size_t TotalBytes = 0; 81 82 if ((iBytes == 0) && (ibuff == NULL) && 83 (obuff == NULL) && (obuff_siz == 0)) { 84 TotalBytes = 0; 85 return 0; 86 } 87 88 if ((ibuff == NULL) || (obuff == NULL) || (iBytes < 1)) return -1; 89 90 memset(obuff, 0, obuff_siz); 91 sprintf(buff,"%08lX ", (long)TotalBytes); 92 strncat(obuff, buff, (obuff_siz-1)-strlen(obuff)); 93 for (i=0;i<iBytes;i++) { 94 sprintf(buff,"%02X", *(ibuff+i)); 95 strncat(obuff, buff, (obuff_siz-1)-strlen(obuff)); 96 } 97 TotalBytes += iBytes; 98 return 0; 99} 100 101int dump_data(FILE *stream, const char *buff, const size_t buff_siz, const size_t ofd_siz, const size_t offset, const int format) 102{ 103 size_t TotalRemainingBytes, NumBytes, ibuff_siz, obuff_siz; 104 char *ibuff, *obuff, *buff_curr; 105 106 buff_curr = (char *) buff; 107 buff_curr += offset; 108 TotalRemainingBytes = buff_siz; 109 NumBytes = 0; 110 ibuff_siz = ofd_siz; 111 obuff_siz = 12+(3*ibuff_siz)+(ibuff_siz/4)+(ibuff_siz/8)+ibuff_siz; 112 switch(format) { 113 case FMT_STR: 114 format_str(0, NULL, 0, NULL, 0); 115 break; 116 case FMT_RAW: 117 format_raw(0, NULL, NULL, 0); 118 break; 119 default: 120 return(-1); 121 } 122 123 if ((ibuff = (char *) ALLOC(ibuff_siz)) == NULL) { 124 fprintf(stderr, "Can't allocate ibuff\n"); 125 return(-1); 126 } 127 if ((obuff = (char *) ALLOC(obuff_siz)) == NULL) { 128 FREE(ibuff); 129 fprintf(stderr, "Can't allocate obuff\n"); 130 return(-1); 131 } 132 133 while (TotalRemainingBytes > 0) { 134 if (TotalRemainingBytes >= ibuff_siz) { 135 memcpy(ibuff, buff_curr, ibuff_siz); 136 TotalRemainingBytes -= ibuff_siz; 137 NumBytes = ibuff_siz; 138 buff_curr += NumBytes; 139 } else { 140 memcpy(ibuff, buff_curr, TotalRemainingBytes); 141 NumBytes = TotalRemainingBytes; 142 TotalRemainingBytes = 0; 143 } 144 switch(format) { 145 case FMT_STR: 146 format_str(NumBytes, ibuff, ibuff_siz, obuff, obuff_siz); 147 fprintf(stream, "%s\n", obuff); 148 break; 149 case FMT_RAW: 150 format_raw(NumBytes, ibuff, obuff, obuff_siz); 151 fprintf(stream, "%s\n", obuff); 152 break; 153 default: 154 return(-1); 155 } 156 } 157 FREE(ibuff); 158 FREE(obuff); 159 return 0; 160} 161 162int do_dump(child_args_t *args) 163{ 164 size_t NumBytes = 0; 165 OFF_T TargetLBA, TotalBytes = 0; 166 char *buff; 167 fd_t fd; 168 169 if ((buff = (char *) ALLOC(args->htrsiz*BLK_SIZE)) == NULL) { 170 fprintf(stderr, "Can't allocate buffer\n"); 171 return(-1); 172 } 173 174 memset(buff, 0, args->htrsiz*BLK_SIZE); 175 176 fd = Open(args->device, args->flags | CLD_FLG_R); 177 if (INVALID_FD(fd)) { 178 pMsg(ERR, args, "could not open %s.\n",args->device); 179 pMsg(ERR, args, "%s: Error = %u\n",args->device, GETLASTERROR()); 180 return(-1); 181 } 182 183 TargetLBA = Seek(fd, args->start_lba*BLK_SIZE); 184 if (TargetLBA != (args->start_lba * (OFF_T) BLK_SIZE)) { 185 pMsg(ERR, args, "Could not seek to start position.\n"); 186 CLOSE(fd); 187 return(-1); 188 } 189 190 do { 191 NumBytes = Read(fd, buff, args->htrsiz*BLK_SIZE); 192 if ((NumBytes > args->htrsiz*BLK_SIZE) || (NumBytes < 0)) { 193 pMsg(ERR, args, "Failure reading %s\n", args->device); 194 pMsg(ERR, args, "Last Error was %lu\n", GETLASTERROR()); 195 break; 196 } 197 dump_data(stdout, buff, NumBytes, 16, 0, FMT_STR); 198 TotalBytes += (OFF_T) NumBytes; 199 } while ((TotalBytes < (args->htrsiz*BLK_SIZE)) && (NumBytes > 0)); 200 201 FREE(buff); 202 CLOSE(fd); 203 204 return 0; 205}