whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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 |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 40 | #include <stdlib.h> |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | static char Errmsg[80]; |
| 44 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 45 | int dataasciigen(char *listofchars, char *buffer, int bsize, int offset) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 46 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 47 | int cnt; |
| 48 | int total; |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 49 | int ind; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 50 | char *chr; |
| 51 | int chars_size; |
| 52 | char *charlist; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 54 | chr = buffer; |
| 55 | total = offset + bsize; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 56 | |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 57 | if (listofchars == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | charlist = CHARS; |
| 59 | chars_size = CHARS_SIZE; |
| 60 | } else { |
| 61 | charlist = listofchars; |
| 62 | chars_size = strlen(listofchars); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | for (cnt = offset; cnt < total; cnt++) { |
| 66 | ind = cnt % chars_size; |
| 67 | *chr++ = charlist[ind]; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return bsize; |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 71 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 72 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 73 | int dataasciichk(char *listofchars, char *buffer, int bsize, |
| 74 | int offset, char **errmsg) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 75 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | int cnt; |
| 77 | int total; |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 78 | int ind; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | char *chr; |
| 80 | int chars_size; |
| 81 | char *charlist; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 82 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | chr = buffer; |
| 84 | total = offset + bsize; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 85 | |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 86 | if (listofchars == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | charlist = CHARS; |
| 88 | chars_size = CHARS_SIZE; |
| 89 | } else { |
| 90 | charlist = listofchars; |
| 91 | chars_size = strlen(listofchars); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 94 | if (errmsg != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | *errmsg = Errmsg; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 96 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | sprintf(Errmsg, "all %d bytes match desired pattern", bsize); |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 108 | return -1; |
| 109 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 110 | |
| 111 | #if UNIT_TEST |
| 112 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 113 | int main(int ac, char **ag) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 114 | { |
| 115 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | int size = 1023; |
| 117 | char *buffer; |
| 118 | int ret; |
| 119 | char *errmsg; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 120 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 121 | buffer = malloc(size); |
| 122 | if (buffer == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | perror("malloc"); |
| 124 | exit(2); |
| 125 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 126 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | dataasciigen(NULL, buffer, size, 0); |
| 128 | printf("dataasciigen(NULL, buffer, %d, 0)\n", size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 129 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | ret = dataasciichk(NULL, buffer, size, 0, &errmsg); |
| 131 | printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n", |
| 132 | size, ret, errmsg); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 133 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 138 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg); |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 140 | printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n", |
| 141 | size - 1, ret, errmsg); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 142 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 147 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | buffer[25] = 0x0; |
| 149 | printf("changing char 25\n"); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 150 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg); |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 152 | printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n", |
| 153 | size - 1, ret, errmsg); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 154 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 159 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | dataasciigen("this is a test of the my string", buffer, size, 0); |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 161 | printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n", |
| 162 | size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 163 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 168 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 173 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | ret = |
| 175 | dataasciichk("this is a test of the my string", &buffer[1], |
| 176 | size - 1, 1, &errmsg); |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 179 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 184 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | buffer[25] = 0x0; |
| 186 | printf("changing char 25\n"); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 187 | |
Wanlong Gao | df5b3b4 | 2012-12-25 15:25:35 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 192 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 197 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 198 | exit(0); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #endif |