1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 
4 /**
5  * \file modp_ascii.c
6  * <PRE>
7  * MODP_ASCII - Ascii transformations (upper/lower, etc)
8  * http://code.google.com/p/stringencoders/
9  *
10  * Copyright &copy; 2007  Nick Galbreath -- nickg [at] modp [dot] com
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are
15  * met:
16  *
17  *   Redistributions of source code must retain the above copyright
18  *   notice, this list of conditions and the following disclaimer.
19  *
20  *   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  *
24  *   Neither the name of the modp.com nor the names of its
25  *   contributors may be used to endorse or promote products derived from
26  *   this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * 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
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * This is the standard "new" BSD license:
41  * http://www.opensource.org/licenses/bsd-license.php
42  * </PRE>
43  */
44 
45 #include <stdint.h>
46 #include "modp_ascii.h"
47 #include "modp_ascii_data.h"
48 
modp_toupper_copy(char * dest,const char * str,int len)49 void modp_toupper_copy(char* dest, const char* str, int len)
50 {
51     int i;
52     uint32_t eax, ebx;
53     const uint8_t* ustr = (const uint8_t*) str;
54     const int leftover = len % 4;
55     const int imax = len / 4;
56     const uint32_t* s = (const uint32_t*) str;
57     uint32_t* d = (uint32_t*) dest;
58     for (i = 0; i != imax; ++i) {
59         eax = s[i];
60         /*
61          * This is based on the algorithm by Paul Hsieh
62          * http://www.azillionmonkeys.com/qed/asmexample.html
63          */
64         ebx = (0x7f7f7f7fu & eax) + 0x05050505u;
65         ebx = (0x7f7f7f7fu & ebx) + 0x1a1a1a1au;
66         ebx = ((ebx & ~eax) >> 2)  & 0x20202020u;
67         *d++ = eax - ebx;
68     }
69 
70     i = imax*4;
71     dest = (char*) d;
72     switch (leftover) {
73     case 3: *dest++ = (char) gsToUpperMap[ustr[i++]];
74     case 2: *dest++ = (char) gsToUpperMap[ustr[i++]];
75     case 1: *dest++ = (char) gsToUpperMap[ustr[i]];
76     case 0: *dest = '\0';
77     }
78 }
79 
modp_tolower_copy(char * dest,const char * str,int len)80 void modp_tolower_copy(char* dest, const char* str, int len)
81 {
82     int i;
83     uint32_t eax, ebx;
84     const uint8_t* ustr = (const uint8_t*) str;
85     const int leftover = len % 4;
86     const int imax = len / 4;
87     const uint32_t* s = (const uint32_t*) str;
88     uint32_t* d = (uint32_t*) dest;
89     for (i = 0; i != imax; ++i) {
90         eax = s[i];
91         /*
92          * This is based on the algorithm by Paul Hsieh
93          * http://www.azillionmonkeys.com/qed/asmexample.html
94          */
95         ebx = (0x7f7f7f7fu & eax) + 0x25252525u;
96         ebx = (0x7f7f7f7fu & ebx) + 0x1a1a1a1au;
97         ebx = ((ebx & ~eax) >> 2)  & 0x20202020u;
98         *d++ = eax + ebx;
99     }
100 
101     i = imax*4;
102     dest = (char*) d;
103     switch (leftover) {
104     case 3: *dest++ = (char) gsToLowerMap[ustr[i++]];
105     case 2: *dest++ = (char) gsToLowerMap[ustr[i++]];
106     case 1: *dest++ = (char) gsToLowerMap[ustr[i]];
107     case 0: *dest = '\0';
108     }
109 }
110 
modp_toupper(char * str,int len)111 void modp_toupper(char* str, int len)
112 {
113     modp_toupper_copy(str, str, len);
114 }
115 
modp_tolower(char * str,int len)116 void modp_tolower(char* str, int len)
117 {
118     modp_tolower_copy(str,str,len);
119 }
120 
modp_toprint_copy(char * dest,const char * str,int len)121 void modp_toprint_copy(char* dest, const char* str, int len)
122 {
123     int i;
124     uint8_t c1,c2,c3,c4;
125 
126     const int leftover = len % 4;
127     const int imax = len - leftover;
128     const uint8_t* s = (const uint8_t*) str;
129     for (i = 0; i != imax ; i+=4) {
130         /*
131          * it's important to make these variables
132          * it helps the optimizer to figure out what to do
133          */
134         c1 = s[i]; c2=s[i+1]; c3=s[i+2]; c4=s[i+3];
135         dest[0] = (char) gsToPrintMap[c1];
136         dest[1] = (char) gsToPrintMap[c2];
137         dest[2] = (char) gsToPrintMap[c3];
138         dest[3] = (char) gsToPrintMap[c4];
139         dest += 4;
140     }
141 
142     switch (leftover) {
143     case 3: *dest++ = (char) gsToPrintMap[s[i++]];
144     case 2: *dest++ = (char) gsToPrintMap[s[i++]];
145     case 1: *dest++ = (char) gsToPrintMap[s[i]];
146     case 0: *dest = '\0';
147     }
148 }
149 
modp_toprint(char * str,int len)150 void modp_toprint(char* str, int len)
151 {
152     modp_toprint_copy(str,str,len);
153 }
154