Home | History | Annotate | Download | only in doio
      1 /*
      2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * Further, this software is distributed without any warranty that it is
     13  * free of the rightful claim of any third person regarding infringement
     14  * or the like.  Any license provided herein, whether implied or
     15  * otherwise, applies only to this software file.  Patent licenses, if
     16  * any, provided herein do not apply to combinations of this program with
     17  * other software, or any other product whatsoever.
     18  *
     19  * You should have received a copy of the GNU General Public License along
     20  * with this program; if not, write the Free Software Foundation, Inc.,
     21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     22  *
     23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
     24  * Mountain View, CA  94043, or:
     25  *
     26  * http://www.sgi.com
     27  *
     28  * For further information regarding this notice, see:
     29  *
     30  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
     31  */
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include "dataascii.h"
     35 
     36 #define CHARS		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz\n"
     37 #define CHARS_SIZE	sizeof(CHARS)
     38 
     39 #ifdef UNIT_TEST
     40 #include <stdlib.h>
     41 #endif
     42 
     43 static char Errmsg[80];
     44 
     45 int dataasciigen(char *listofchars, char *buffer, int bsize, int offset)
     46 {
     47 	int cnt;
     48 	int total;
     49 	int ind;
     50 	char *chr;
     51 	int chars_size;
     52 	char *charlist;
     53 
     54 	chr = buffer;
     55 	total = offset + bsize;
     56 
     57 	if (listofchars == NULL) {
     58 		charlist = CHARS;
     59 		chars_size = CHARS_SIZE;
     60 	} else {
     61 		charlist = listofchars;
     62 		chars_size = strlen(listofchars);
     63 	}
     64 
     65 	for (cnt = offset; cnt < total; cnt++) {
     66 		ind = cnt % chars_size;
     67 		*chr++ = charlist[ind];
     68 	}
     69 
     70 	return bsize;
     71 }
     72 
     73 int dataasciichk(char *listofchars, char *buffer, int bsize,
     74 		 int offset, char **errmsg)
     75 {
     76 	int cnt;
     77 	int total;
     78 	int ind;
     79 	char *chr;
     80 	int chars_size;
     81 	char *charlist;
     82 
     83 	chr = buffer;
     84 	total = offset + bsize;
     85 
     86 	if (listofchars == NULL) {
     87 		charlist = CHARS;
     88 		chars_size = CHARS_SIZE;
     89 	} else {
     90 		charlist = listofchars;
     91 		chars_size = strlen(listofchars);
     92 	}
     93 
     94 	if (errmsg != NULL)
     95 		*errmsg = Errmsg;
     96 
     97 	for (cnt = offset; cnt < total; chr++, cnt++) {
     98 		ind = cnt % chars_size;
     99 		if (*chr != charlist[ind]) {
    100 			sprintf(Errmsg,
    101 				"data mismatch at offset %d, exp:%#o, act:%#o",
    102 				cnt, charlist[ind], *chr);
    103 			return cnt;
    104 		}
    105 	}
    106 
    107 	sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
    108 	return -1;
    109 }
    110 
    111 #if UNIT_TEST
    112 
    113 int main(int ac, char **ag)
    114 {
    115 
    116 	int size = 1023;
    117 	char *buffer;
    118 	int ret;
    119 	char *errmsg;
    120 
    121 	buffer = malloc(size);
    122 	if (buffer == NULL) {
    123 		perror("malloc");
    124 		exit(2);
    125 	}
    126 
    127 	dataasciigen(NULL, buffer, size, 0);
    128 	printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
    129 
    130 	ret = dataasciichk(NULL, buffer, size, 0, &errmsg);
    131 	printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
    132 	       size, ret, errmsg);
    133 
    134 	if (ret == -1)
    135 		printf("\tPASS return value is -1 as expected\n");
    136 	else
    137 		printf("\tFAIL return value is %d, expected -1\n", ret);
    138 
    139 	ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
    140 	printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
    141 		size - 1, ret, errmsg);
    142 
    143 	if (ret == -1)
    144 		printf("\tPASS return value is -1 as expected\n");
    145 	else
    146 		printf("\tFAIL return value is %d, expected -1\n", ret);
    147 
    148 	buffer[25] = 0x0;
    149 	printf("changing char 25\n");
    150 
    151 	ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
    152 	printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
    153 		size - 1, ret, errmsg);
    154 
    155 	if (ret == 25)
    156 		printf("\tPASS return value is 25 as expected\n");
    157 	else
    158 		printf("\tFAIL return value is %d, expected 25\n", ret);
    159 
    160 	dataasciigen("this is a test of the my string", buffer, size, 0);
    161 	printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n",
    162 		size);
    163 
    164 	ret = dataasciichk("this is a test of the my string",
    165 			   buffer, size, 0, &errmsg);
    166 	printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
    167 		size, ret, errmsg);
    168 
    169 	if (ret == -1)
    170 		printf("\tPASS return value is -1 as expected\n");
    171 	else
    172 		printf("\tFAIL return value is %d, expected -1\n", ret);
    173 
    174 	ret =
    175 	    dataasciichk("this is a test of the my string", &buffer[1],
    176 			 size - 1, 1, &errmsg);
    177 	printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
    178 		size - 1, ret, errmsg);
    179 
    180 	if (ret == -1)
    181 		printf("\tPASS return value is -1 as expected\n");
    182 	else
    183 		printf("\tFAIL return value is %d, expected -1\n", ret);
    184 
    185 	buffer[25] = 0x0;
    186 	printf("changing char 25\n");
    187 
    188 	ret = dataasciichk("this is a test of the my string", &buffer[1],
    189 			   size - 1, 1, &errmsg);
    190 	printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
    191 		size - 1, ret, errmsg);
    192 
    193 	if (ret == 25)
    194 		printf("\tPASS return value is 25 as expected\n");
    195 	else
    196 		printf("\tFAIL return value is %d, expected 25\n", ret);
    197 
    198 	exit(0);
    199 }
    200 
    201 #endif
    202