common: prevent buffer overflow
[supercollider.git] / include / server / SC_Str4.h
blob760d1d89115545e11af826dc5ca0d813aa0c5e60
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* a 4 byte aligned and zero padded string allows faster string operations. */
23 #ifndef _SC_Str4_
24 #define _SC_Str4_
26 #include "Hash.h"
27 #include <stdio.h>
28 #include <limits.h>
30 #ifndef _LASTCHAR_
31 #define _LASTCHAR_
32 #if BYTE_ORDER == LITTLE_ENDIAN
33 const int32 kLASTCHAR = 0xFF000000;
34 #else
35 const int32 kLASTCHAR = 0x000000FF;
36 #endif
37 #endif
40 void str4cpy(int32 *dst, const char *src);
41 void mem4cpy(int32 *dst, const char *src, int charlen);
43 // returns the number of pad bytes to add to a string of a given length
44 inline int str4padbytes(int charlen)
46 return 4 - (charlen & 3);
49 // converts length in bytes to length in words
50 inline int str4len(int charlen)
52 return (charlen + 4) >> 2;
55 // returns length in words of a char *
56 inline int str4len(const char *src)
58 const char *src0 = src;
59 while (*src) { src++; }
60 return str4len(src - src0);
63 // returns length in words of a int32 *
64 inline int str4len(const int32 *src)
66 const int32 *src0 = src;
67 while (*src++ & kLASTCHAR) {}
68 int wordlen = src - src0;
69 return wordlen;
72 // returns length in words of a int32 *
73 inline bool str4eq(const int32 *a, const int32 *b)
75 while(true) {
76 if (*a != *b) return false;
77 if ((*a & kLASTCHAR) == 0) return true;
78 a++; b++;
82 // copy an int32 *
83 inline void str4cpy(int32 *dst, const int32 *src)
85 int32 c;
86 do {
87 *dst++ = c = *src++;
88 } while (c & kLASTCHAR);
91 inline int sc_atoi(const char *string)
93 int value = 0;
94 if (*string == 0) return -1;
95 uint32 c;
96 while ((c = *string++ - '0') <= 9) {
97 value = value * 10 + c;
99 return value;
103 #endif