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