]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/ctypeio.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / LibC / Locale / ctypeio.c
1 /** @file
2 Internal C-type locale functions.
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Copyright (c) 1997 Christos Zoulas. All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. All advertising materials mentioning features or use of this software
24 must display the following acknowledgement:
25 This product includes software developed by Christos Zoulas.
26 4. The name of the author may not be used to endorse or promote products
27 derived from this software without specific prior written permission.
28
29 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
40 NetBSD: ctypeio.c,v 1.7 2005/11/29 03:11:59 christos Exp
41 **/
42 #include <LibConfig.h>
43 #include <sys/EfiCdefs.h>
44 #if defined(LIBC_SCCS) && !defined(lint)
45 __RCSID("$NetBSD: ctypeio.c,v 1.7 2005/11/29 03:11:59 christos Exp $");
46 #endif /* LIBC_SCCS and not lint */
47
48 #include <sys/types.h>
49
50 #include <assert.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #define _CTYPE_PRIVATE
55 #include <ctype.h>
56 #include "ctypeio.h"
57
58 int
59 __loadctype(const char *name)
60 {
61 FILE *fp;
62 char id[sizeof(_CTYPE_ID) - 1];
63 u_int32_t i, len;
64 unsigned short *new_ctype = NULL;
65 unsigned char *new_toupper = NULL, *new_tolower = NULL;
66
67 _DIAGASSERT(name != NULL);
68
69 if ((fp = fopen(name, "r")) == NULL)
70 return 0;
71
72 if (fread(id, sizeof(id), 1, fp) != 1)
73 goto bad;
74
75 if (memcmp(id, _CTYPE_ID, sizeof(id)) != 0)
76 goto bad;
77
78 if (fread(&i, sizeof(u_int32_t), 1, fp) != 1)
79 goto bad;
80
81 if ((i = ntohl(i)) != _CTYPE_REV)
82 goto bad;
83
84 if (fread(&len, sizeof(u_int32_t), 1, fp) != 1)
85 goto bad;
86
87 if ((len = ntohl(len)) != _CTYPE_NUM_CHARS)
88 goto bad;
89
90 if ((new_ctype = malloc(sizeof(UINT16) * (1 + len))) == NULL)
91 goto bad;
92
93 new_ctype[0] = 0;
94 if (fread(&new_ctype[1], sizeof(UINT16), len, fp) != len)
95 goto bad;
96
97 if ((new_toupper = malloc(sizeof(UINT8) * (1 + len))) == NULL)
98 goto bad;
99
100 new_toupper[0] = (UINT8)EOF;
101 if (fread(&new_toupper[1], sizeof(UINT8), len, fp) != len)
102 goto bad;
103
104 if ((new_tolower = malloc(sizeof(UINT8) * (1 + len))) == NULL)
105 goto bad;
106
107 new_tolower[0] = (UINT8)EOF;
108 if (fread(&new_tolower[1], sizeof(UINT8), len, fp) != len)
109 goto bad;
110
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 for (i = 1; i <= len; i++) {
113 new_ctype[i] = ntohs(new_ctype[i]);
114 }
115 #endif
116
117 (void) fclose(fp);
118 if (_cClass != _C_CharClassTable)
119 free(__UNCONST(_cClass));
120 _cClass = new_ctype;
121 if (_uConvT != _C_ToUpperTable)
122 free(__UNCONST(_uConvT));
123 _uConvT = new_toupper;
124 if (_lConvT != _C_ToLowerTable)
125 free(__UNCONST(_lConvT));
126 _lConvT = new_tolower;
127
128 return 1;
129 bad:
130 free(new_tolower);
131 free(new_toupper);
132 free(new_ctype);
133 (void) fclose(fp);
134 return 0;
135 }
136
137 int
138 __savectype(
139 const char *name,
140 unsigned short *new_ctype,
141 unsigned char *new_toupper,
142 unsigned char *new_tolower
143 )
144 {
145 FILE *fp;
146 u_int32_t i, len = _CTYPE_NUM_CHARS;
147
148 _DIAGASSERT(name != NULL);
149 _DIAGASSERT(new_ctype != NULL);
150 _DIAGASSERT(new_toupper != NULL);
151 _DIAGASSERT(new_tolower != NULL);
152
153 if ((fp = fopen(name, "w")) == NULL)
154 return 0;
155
156 if (fwrite(_CTYPE_ID, sizeof(_CTYPE_ID) - 1, 1, fp) != 1)
157 goto bad;
158
159 i = htonl(_CTYPE_REV);
160 if (fwrite(&i, sizeof(u_int32_t), 1, fp) != 1)
161 goto bad;
162
163 i = htonl(len);
164 if (fwrite(&i, sizeof(u_int32_t), 1, fp) != 1)
165 goto bad;
166
167 #if BYTE_ORDER == LITTLE_ENDIAN
168 for (i = 1; i <= len; i++) {
169 new_ctype[i] = htons(new_ctype[i]);
170 }
171 #endif
172 if (fwrite(&new_ctype[1], sizeof(UINT16), len, fp) != len)
173 goto bad;
174
175 if (fwrite(&new_toupper[1], sizeof(UINT8), len, fp) != len)
176 goto bad;
177
178 if (fwrite(&new_tolower[1], sizeof(UINT8), len, fp) != len)
179 goto bad;
180
181 (void) fclose(fp);
182 return 1;
183 bad:
184 (void) fclose(fp);
185 return 0;
186 }