1 /*
2 ** The OpenGL Extension Wrangler Library
3 ** Copyright (C) 2002-2008, Milan Ikits <milan ikits[]ieee org>
4 ** Copyright (C) 2002-2008, Marcelo E. Magallon <mmagallo[]debian org>
5 ** Copyright (C) 2002, Lev Povalahev
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions are met:
10 **
11 ** * Redistributions of source code must retain the above copyright notice,
12 **   this list of conditions and the following disclaimer.
13 ** * Redistributions in binary form must reproduce the above copyright notice,
14 **   this list of conditions and the following disclaimer in the documentation
15 **   and/or other materials provided with the distribution.
16 ** * The name of the author may be used to endorse or promote products
17 **   derived from this software without specific prior written permission.
18 **
19 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 ** THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #include <GL/glew.h>
33 #if defined(_WIN32)
34 #  include <GL/wglew.h>
35 #elif !defined(__ANDROID__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX))
36 #  include <GL/glxew.h>
37 #endif
38 
39 #include "glew_utils.h"
40 
41 #if defined(__APPLE__)
42 #include <mach-o/dyld.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
NSGLGetProcAddress(const GLubyte * name)46 void* NSGLGetProcAddress (const GLubyte *name)
47 {
48   NSSymbol symbol;
49   char* symbolName;
50   /* prepend a '_' for the Unix C symbol mangling convention */
51   symbolName = malloc(strlen((const char*)name) + 2);
52   strcpy(symbolName+1, (const char*)name);
53   symbolName[0] = '_';
54   symbol = NULL;
55   if (NSIsSymbolNameDefined(symbolName))
56     symbol = NSLookupAndBindSymbol(symbolName);
57   free(symbolName);
58   return symbol ? NSAddressOfSymbol(symbol) : NULL;
59 }
60 #endif /* __APPLE__ */
61 
62 #if defined(__sgi) || defined (__sun)
63 #include <dlfcn.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 
dlGetProcAddress(const GLubyte * name)67 void* dlGetProcAddress (const GLubyte* name)
68 {
69   static void* h = NULL;
70   static void* gpa;
71 
72   if (h == NULL)
73   {
74     if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
75     gpa = dlsym(h, "glXGetProcAddress");
76   }
77 
78   if (gpa != NULL)
79     return ((void*(*)(const GLubyte*))gpa)(name);
80   else
81     return dlsym(h, (const char*)name);
82 }
83 #endif /* __sgi || __sun */
84 
85 /*
86  * GLEW, just like OpenGL or GLU, does not rely on the standard C library.
87  * These functions implement the functionality required in this file.
88  */
89 
_glewStrLen(const GLubyte * s)90 GLuint _glewStrLen (const GLubyte* s)
91 {
92   GLuint i=0;
93   while (s+i != NULL && s[i] != '\0') i++;
94   return i;
95 }
96 
_glewStrCLen(const GLubyte * s,GLubyte c)97 GLuint _glewStrCLen (const GLubyte* s, GLubyte c)
98 {
99   GLuint i=0;
100   while (s+i != NULL && s[i] != '\0' && s[i] != c) i++;
101   return i;
102 }
103 
_glewStrSame(const GLubyte * a,const GLubyte * b,GLuint n)104 GLboolean _glewStrSame (const GLubyte* a, const GLubyte* b, GLuint n)
105 {
106   GLuint i=0;
107   while (i < n && a+i != NULL && b+i != NULL && a[i] == b[i]) i++;
108   return i == n ? GL_TRUE : GL_FALSE;
109 }
110 
_glewStrSame1(GLubyte ** a,GLuint * na,const GLubyte * b,GLuint nb)111 GLboolean _glewStrSame1 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
112 {
113   while (*na > 0 && (**a == ' ' || **a == '\n' || **a == '\r' || **a == '\t'))
114   {
115     *a++;
116     *na--;
117   }
118   if(*na >= nb)
119   {
120     GLuint i=0;
121     while (i < nb && *a+i != NULL && b+i != NULL && *a[i] == b[i]) i++;
122 	if(i == nb)
123 	{
124 		*a = *a + nb;
125 		*na = *na - nb;
126 		return GL_TRUE;
127 	}
128   }
129   return GL_FALSE;
130 }
131 
_glewStrSame2(GLubyte ** a,GLuint * na,const GLubyte * b,GLuint nb)132 GLboolean _glewStrSame2 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
133 {
134   if(*na >= nb)
135   {
136     GLuint i=0;
137     while (i < nb && *a+i != NULL && b+i != NULL && *a[i] == b[i]) i++;
138 	if(i == nb)
139 	{
140 		*a = *a + nb;
141 		*na = *na - nb;
142 		return GL_TRUE;
143 	}
144   }
145   return GL_FALSE;
146 }
147 
_glewStrSame3(GLubyte ** a,GLuint * na,const GLubyte * b,GLuint nb)148 GLboolean _glewStrSame3 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
149 {
150   if(*na >= nb)
151   {
152     GLuint i=0;
153     while (i < nb && *a+i != NULL && b+i != NULL && *a[i] == b[i]) i++;
154     if (i == nb && (*na == nb || *a[i] == ' ' || *a[i] == '\n' || *a[i] == '\r' || *a[i] == '\t'))
155     {
156       *a = *a + nb;
157       *na = *na - nb;
158       return GL_TRUE;
159     }
160   }
161   return GL_FALSE;
162 }
163