Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: third_party/modp_b64/modp_b64.cc

Issue 1354743002: Avoid misaligned read/write on little endian platforms (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/modp_b64/README.chromium ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ 1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */ 2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 /** 3 /**
4 * \file 4 * \file
5 * <PRE> 5 * <PRE>
6 * MODP_B64 - High performance base64 encoder/decoder 6 * MODP_B64 - High performance base64 encoder/decoder
7 * Version 1.3 -- 17-Mar-2006 7 * Version 1.3 -- 17-Mar-2006
8 * http://modp.com/release/base64 8 * http://modp.com/release/base64
9 * 9 *
10 * Copyright &copy; 2005, 2006 Nick Galbreath -- nickg [at] modp [dot] com 10 * Copyright &copy; 2005, 2006 Nick Galbreath -- nickg [at] modp [dot] com
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 204 }
205 } 205 }
206 #endif 206 #endif
207 207
208 size_t i; 208 size_t i;
209 int leftover = len % 4; 209 int leftover = len % 4;
210 size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4; 210 size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
211 211
212 uint8_t* p = (uint8_t*)dest; 212 uint8_t* p = (uint8_t*)dest;
213 uint32_t x = 0; 213 uint32_t x = 0;
214 uint32_t* destInt = (uint32_t*) p; 214 const uint8_t* y = (uint8_t*)src;
215 uint32_t* srcInt = (uint32_t*) src; 215 src += 4;
Alex Vakulenko (Google) 2015/09/17 21:15:17 Why do we even care about src from this point on?
Vitaly Buka (NO REVIEWS) 2015/09/17 21:19:42 Done.
216 uint32_t y = *srcInt++;
217 for (i = 0; i < chunks; ++i) { 216 for (i = 0; i < chunks; ++i) {
218 x = d0[y & 0xff] | 217 x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
219 d1[(y >> 8) & 0xff] |
220 d2[(y >> 16) & 0xff] |
221 d3[(y >> 24) & 0xff];
222 218
223 if (x >= BADCHAR) return MODP_B64_ERROR; 219 if (x >= BADCHAR) return MODP_B64_ERROR;
224 *destInt = x ; 220 *p++ = ((uint8_t*)(&x))[0];
225 p += 3; 221 *p++ = ((uint8_t*)(&x))[1];
226 destInt = (uint32_t*)p; 222 *p++ = ((uint8_t*)(&x))[2];
227 y = *srcInt++;} 223 y = (uint8_t*)src;
224 src += 4;}
228 225
229 226
230 switch (leftover) { 227 switch (leftover) {
231 case 0: 228 case 0:
232 x = d0[y & 0xff] | 229 x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
233 d1[(y >> 8) & 0xff] |
234 d2[(y >> 16) & 0xff] |
235 d3[(y >> 24) & 0xff];
236 230
237 if (x >= BADCHAR) return MODP_B64_ERROR; 231 if (x >= BADCHAR) return MODP_B64_ERROR;
238 *p++ = ((uint8_t*)(&x))[0]; 232 *p++ = ((uint8_t*)(&x))[0];
239 *p++ = ((uint8_t*)(&x))[1]; 233 *p++ = ((uint8_t*)(&x))[1];
240 *p = ((uint8_t*)(&x))[2]; 234 *p = ((uint8_t*)(&x))[2];
241 return (chunks+1)*3; 235 return (chunks+1)*3;
242 break; 236 break;
243 case 1: /* with padding this is an impossible case */ 237 case 1: /* with padding this is an impossible case */
244 x = d0[y & 0xff]; 238 x = d0[y[0]];
245 *p = *((uint8_t*)(&x)); // i.e. first char/byte in int 239 *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
246 break; 240 break;
247 case 2: // * case 2, 1 output byte */ 241 case 2: // * case 2, 1 output byte */
248 x = d0[y & 0xff] | d1[y >> 8 & 0xff]; 242 x = d0[y[0]] | d1[y[1]];
249 *p = *((uint8_t*)(&x)); // i.e. first char 243 *p = *((uint8_t*)(&x)); // i.e. first char
250 break; 244 break;
251 default: /* case 3, 2 output bytes */ 245 default: /* case 3, 2 output bytes */
252 x = d0[y & 0xff] | 246 x = d0[y[0]] | d1[y[1]] | d2[y[2]]; /* 0x3c */
253 d1[y >> 8 & 0xff ] |
254 d2[y >> 16 & 0xff]; /* 0x3c */
255 *p++ = ((uint8_t*)(&x))[0]; 247 *p++ = ((uint8_t*)(&x))[0];
256 *p = ((uint8_t*)(&x))[1]; 248 *p = ((uint8_t*)(&x))[1];
257 break; 249 break;
258 } 250 }
259 251
260 if (x >= BADCHAR) return MODP_B64_ERROR; 252 if (x >= BADCHAR) return MODP_B64_ERROR;
261 253
262 return 3*chunks + (6*leftover)/8; 254 return 3*chunks + (6*leftover)/8;
263 } 255 }
264 256
265 #endif /* if bigendian / else / endif */ 257 #endif /* if bigendian / else / endif */
OLDNEW
« no previous file with comments | « third_party/modp_b64/README.chromium ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698