lib/: spdx license conversion
[xfstests-dev.git] / lib / dataascii.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include "dataascii.h"
9
10 #define CHARS           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz\n"
11 #define CHARS_SIZE      sizeof(CHARS)
12
13 #ifdef UNIT_TEST
14 #include <stdlib.h> /* malloc */
15 #endif
16
17 static char Errmsg[80];
18
19 int
20 dataasciigen(listofchars, buffer, bsize, offset)
21 char *listofchars;      /* a null terminated list of characters */
22 char *buffer;
23 int bsize;
24 int offset;
25 {
26    int cnt;
27    int total;
28    int ind;     /* index into CHARS array */
29    char *chr;
30    int chars_size;
31    char *charlist;
32
33         chr=buffer;
34         total=offset+bsize;
35
36         if ( listofchars == NULL ) {
37             charlist=CHARS;
38             chars_size=CHARS_SIZE;
39         }
40         else {
41             charlist=listofchars;
42             chars_size=strlen(listofchars);
43         }
44
45         for(cnt=offset; cnt<total;  cnt++) {
46                 ind=cnt%chars_size;
47                 *chr++=charlist[ind];
48         }
49
50         return bsize;
51
52 }       /* end of dataasciigen */
53
54 int
55 dataasciichk(listofchars, buffer, bsize, offset, errmsg)
56 char *listofchars;      /* a null terminated list of characters */
57 char *buffer;
58 int bsize;
59 int offset;
60 char **errmsg;
61 {
62    int cnt;
63    int total;
64    int ind;     /* index into CHARS array */
65    char *chr;
66    int chars_size;
67    char *charlist;
68
69         chr=buffer;
70         total=offset+bsize;
71
72         if ( listofchars == NULL ) {
73             charlist=CHARS;
74             chars_size=CHARS_SIZE;
75         }
76         else {
77             charlist=listofchars;
78             chars_size=strlen(listofchars);
79         }
80
81         if ( errmsg != NULL ) {
82             *errmsg = Errmsg;
83         }
84
85         for(cnt=offset; cnt<total;  chr++, cnt++) {
86             ind=cnt%chars_size;
87             if ( *chr != charlist[ind] ) {
88                 sprintf(Errmsg,
89                     "data mismatch at offset %d, exp:%#o, act:%#o", cnt,
90                     charlist[ind], *chr);
91                 return cnt;
92             }
93         }
94
95         sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
96         return -1;      /* buffer is ok */
97
98 }       /* end of dataasciichk */
99
100
101 #if UNIT_TEST
102
103 /***********************************************************************
104  * main for doing unit testing
105  ***********************************************************************/
106 int
107 main(ac, ag)
108 int ac;
109 char **ag;
110 {
111
112 int size=1023;
113 char *buffer;
114 int ret;
115 char *errmsg;
116
117     if ((buffer=(char *)malloc(size)) == NULL ) {
118         perror("malloc");
119         exit(2);
120     }
121
122     dataasciigen(NULL, buffer, size, 0);
123     printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
124
125     ret=dataasciichk(NULL, buffer, size, 0, &errmsg);
126     printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
127         size, ret, errmsg);
128
129     if ( ret == -1 )
130         printf("\tPASS return value is -1 as expected\n");
131     else
132         printf("\tFAIL return value is %d, expected -1\n", ret);
133
134     ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
135     printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
136         size-1, ret, errmsg);
137
138     if ( ret == -1 )
139         printf("\tPASS return value is -1 as expected\n");
140     else
141         printf("\tFAIL return value is %d, expected -1\n", ret);
142
143     buffer[25]= 0x0;
144     printf("changing char 25\n");
145
146     ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
147     printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
148         size-1, ret, errmsg);
149
150     if ( ret == 25 )
151         printf("\tPASS return value is 25 as expected\n");
152     else
153         printf("\tFAIL return value is %d, expected 25\n", ret);
154
155     dataasciigen("this is a test of the my string" , buffer, size, 0);
156     printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n", size);
157
158     ret=dataasciichk("this is a test of the my string", buffer, size, 0, &errmsg);
159     printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
160         size, ret, errmsg);
161
162     if ( ret == -1 )
163         printf("\tPASS return value is -1 as expected\n");
164     else
165         printf("\tFAIL return value is %d, expected -1\n", ret);
166
167     ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
168     printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
169         size-1, ret, errmsg);
170
171     if ( ret == -1 )
172         printf("\tPASS return value is -1 as expected\n");
173     else
174         printf("\tFAIL return value is %d, expected -1\n", ret);
175
176     buffer[25]= 0x0;
177     printf("changing char 25\n");
178
179     ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
180     printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
181         size-1, ret, errmsg);
182
183     if ( ret == 25 )
184         printf("\tPASS return value is 25 as expected\n");
185     else
186         printf("\tFAIL return value is %d, expected 25\n", ret);
187
188     exit(0);
189 }
190
191 #endif
192