~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to include/grub/misc.h

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-09-13 18:02:04 UTC
  • mfrom: (1.17.15 upstream)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 145.
  • Revision ID: package-import@ubuntu.com-20120913180204-mojnmocbimlom4im
Tags: upstream-2.00
ImportĀ upstreamĀ versionĀ 2.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <grub/types.h>
25
25
#include <grub/symbol.h>
26
26
#include <grub/err.h>
 
27
#include <grub/i18n.h>
27
28
 
28
29
/* GCC version checking borrowed from glibc. */
29
30
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
38
39
#  define ATTRIBUTE_ERROR(msg) \
39
40
        __attribute__ ((__error__ (msg)))
40
41
#else
41
 
#  define ATTRIBUTE_ERROR(msg)
 
42
#  define ATTRIBUTE_ERROR(msg) __attribute__ ((noreturn))
42
43
#endif
43
44
 
44
45
#define ALIGN_UP(addr, align) \
45
46
        ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
 
47
#define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
46
48
#define ALIGN_DOWN(addr, align) \
47
49
        ((addr) & ~((typeof (addr)) align - 1))
48
50
#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
49
51
#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
50
52
 
51
53
#define grub_dprintf(condition, fmt, args...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, fmt, ## args)
52
 
/* XXX: If grub_memmove is too slow, we must implement grub_memcpy.  */
53
 
#define grub_memcpy(d,s,n)      grub_memmove ((d), (s), (n))
54
54
 
55
55
void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
56
56
char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
57
57
char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
58
 
char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
 
58
static inline char *
 
59
grub_stpcpy (char *dest, const char *src)
 
60
{
 
61
  char *d = dest;
 
62
  const char *s = src;
 
63
 
 
64
  do
 
65
    *d++ = *s;
 
66
  while (*s++ != '\0');
 
67
 
 
68
  return d - 1;
 
69
}
 
70
 
 
71
/* XXX: If grub_memmove is too slow, we must implement grub_memcpy.  */
 
72
static inline void *
 
73
grub_memcpy (void *dest, const void *src, grub_size_t n)
 
74
{
 
75
  return grub_memmove (dest, src, n);
 
76
}
59
77
 
60
78
static inline char *
61
79
grub_strcat (char *dest, const char *src)
82
100
  while (*p)
83
101
    p++;
84
102
 
85
 
  while ((*p = *src) != '\0' && c--)
 
103
  while (c-- && (*p = *src) != '\0')
86
104
    {
87
105
      p++;
88
106
      src++;
95
113
 
96
114
/* Prototypes for aliases.  */
97
115
#ifndef GRUB_UTIL
 
116
#ifdef __APPLE__
 
117
int __attribute__ ((regparm(0))) EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
 
118
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
 
119
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
 
120
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
 
121
#else
98
122
int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
99
123
void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
100
124
void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
101
125
void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
102
126
#endif
 
127
#endif
103
128
 
104
129
int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
105
130
int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
108
133
char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
109
134
char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
110
135
int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
111
 
char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle);
 
136
 
 
137
/* Copied from gnulib.
 
138
   Written by Bruno Haible <bruno@clisp.org>, 2005. */
 
139
static inline char *
 
140
grub_strstr (const char *haystack, const char *needle)
 
141
{
 
142
  /* Be careful not to look at the entire extent of haystack or needle
 
143
     until needed.  This is useful because of these two cases:
 
144
       - haystack may be very long, and a match of needle found early,
 
145
       - needle may be very long, and not even a short initial segment of
 
146
       needle may be found in haystack.  */
 
147
  if (*needle != '\0')
 
148
    {
 
149
      /* Speed up the following searches of needle by caching its first
 
150
         character.  */
 
151
      char b = *needle++;
 
152
 
 
153
      for (;; haystack++)
 
154
        {
 
155
          if (*haystack == '\0')
 
156
            /* No match.  */
 
157
            return 0;
 
158
          if (*haystack == b)
 
159
            /* The first character matches.  */
 
160
            {
 
161
              const char *rhaystack = haystack + 1;
 
162
              const char *rneedle = needle;
 
163
 
 
164
              for (;; rhaystack++, rneedle++)
 
165
                {
 
166
                  if (*rneedle == '\0')
 
167
                    /* Found a match.  */
 
168
                    return (char *) haystack;
 
169
                  if (*rhaystack == '\0')
 
170
                    /* No match.  */
 
171
                    return 0;
 
172
                  if (*rhaystack != *rneedle)
 
173
                    /* Nothing in this round.  */
 
174
                    break;
 
175
                }
 
176
            }
 
177
        }
 
178
    }
 
179
  else
 
180
    return (char *) haystack;
 
181
}
 
182
 
112
183
int EXPORT_FUNC(grub_isspace) (int c);
113
184
int EXPORT_FUNC(grub_isprint) (int c);
114
185
 
125
196
}
126
197
 
127
198
static inline int
 
199
grub_islower (int c)
 
200
{
 
201
  return (c >= 'a' && c <= 'z');
 
202
}
 
203
 
 
204
static inline int
 
205
grub_isupper (int c)
 
206
{
 
207
  return (c >= 'A' && c <= 'Z');
 
208
}
 
209
 
 
210
static inline int
128
211
grub_isgraph (int c)
129
212
{
130
213
  return (c >= '!' && c <= '~');
137
220
}
138
221
 
139
222
static inline int
 
223
grub_isxdigit (int c)
 
224
{
 
225
  return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
 
226
}
 
227
 
 
228
static inline int
140
229
grub_isalnum (int c)
141
230
{
142
231
  return grub_isalpha (c) || grub_isdigit (c);
165
254
{
166
255
  while (*s1 && *s2)
167
256
    {
168
 
      if (grub_tolower (*s1) != grub_tolower (*s2))
 
257
      if (grub_tolower ((grub_uint8_t) *s1)
 
258
          != grub_tolower ((grub_uint8_t) *s2))
169
259
        break;
170
260
 
171
261
      s1++;
172
262
      s2++;
173
263
    }
174
264
 
175
 
  return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
 
265
  return (int) grub_tolower ((grub_uint8_t) *s1)
 
266
    - (int) grub_tolower ((grub_uint8_t) *s2);
176
267
}
177
268
 
178
269
static inline int
190
281
      s2++;
191
282
    }
192
283
 
193
 
  return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
194
 
}
195
 
 
196
 
/* Replace all `ch' characters of `input' with `with' and copy the
197
 
   result into `output'; return EOS address of `output'. */
198
 
static inline char *
199
 
grub_strchrsub (char *output, const char *input, char ch, const char *with)
200
 
{
201
 
  grub_size_t grub_strlen (const char *s);
202
 
  while (*input)
203
 
    {
204
 
      if (*input == ch)
205
 
        {
206
 
          grub_strcpy (output, with);
207
 
          output += grub_strlen (with);
208
 
          input++;
209
 
          continue;
210
 
        }
211
 
      *output++ = *input++;
212
 
    }
213
 
  *output = '\0';
214
 
  return output;
 
284
  return (int) grub_tolower ((grub_uint8_t) *s1)
 
285
    - (int) grub_tolower ((grub_uint8_t) *s2);
215
286
}
216
287
 
217
288
unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
237
308
    {
238
309
      if (magnitude > (unsigned long) GRUB_LONG_MAX + 1)
239
310
        {
240
 
          grub_error (GRUB_ERR_OUT_OF_RANGE, "negative overflow");
 
311
          grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
241
312
          return GRUB_LONG_MIN;
242
313
        }
243
314
      return -((long) magnitude);
246
317
    {
247
318
      if (magnitude > GRUB_LONG_MAX)
248
319
        {
249
 
          grub_error (GRUB_ERR_OUT_OF_RANGE, "positive overflow");
 
320
          grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
250
321
          return GRUB_LONG_MAX;
251
322
        }
252
323
      return (long) magnitude;
260
331
int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
261
332
int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
262
333
 
 
334
/* Replace all `ch' characters of `input' with `with' and copy the
 
335
   result into `output'; return EOS address of `output'. */
 
336
static inline char *
 
337
grub_strchrsub (char *output, const char *input, char ch, const char *with)
 
338
{
 
339
  while (*input)
 
340
    {
 
341
      if (*input == ch)
 
342
        {
 
343
          grub_strcpy (output, with);
 
344
          output += grub_strlen (with);
 
345
          input++;
 
346
          continue;
 
347
        }
 
348
      *output++ = *input++;
 
349
    }
 
350
  *output = '\0';
 
351
  return output;
 
352
}
 
353
 
263
354
extern void (*EXPORT_VAR (grub_xputs)) (const char *str);
264
355
 
265
356
static inline int
287
378
char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __attribute__ ((warn_unused_result));
288
379
void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
289
380
void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
290
 
grub_uint64_t EXPORT_FUNC(grub_divmod64_full) (grub_uint64_t n,
291
 
                                               grub_uint64_t d,
292
 
                                               grub_uint64_t *r);
293
 
static inline grub_uint64_t grub_divmod64 (grub_uint64_t n,
294
 
                                           grub_uint32_t d,
295
 
                                           grub_uint32_t *r)
296
 
{
297
 
  grub_uint64_t ret, rr;
298
 
  
299
 
  ret = grub_divmod64_full (n, d, &rr);
300
 
  if (r)
301
 
    *r = rr;
302
 
  return ret;
303
 
}
 
381
grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
 
382
                                          grub_uint64_t d,
 
383
                                          grub_uint64_t *r);
304
384
 
305
 
#if NEED_ENABLE_EXECUTE_STACK && !defined(GRUB_UTIL)
 
385
#if !defined(GRUB_UTIL) && NEED_ENABLE_EXECUTE_STACK
306
386
void EXPORT_FUNC(__enable_execute_stack) (void *addr);
307
387
#endif
308
388
 
309
 
#if NEED_REGISTER_FRAME_INFO && !defined(GRUB_UTIL)
 
389
#if !defined(GRUB_UTIL) && NEED_REGISTER_FRAME_INFO
310
390
void EXPORT_FUNC (__register_frame_info) (void);
311
391
void EXPORT_FUNC (__deregister_frame_info) (void);
312
392
#endif
313
393
 
314
394
/* Inline functions.  */
315
395
 
 
396
static inline char *
 
397
grub_memchr (const void *p, int c, grub_size_t len)
 
398
{
 
399
  const char *s = p;
 
400
  const char *e = s + len;
 
401
 
 
402
  for (; s < e; s++)
 
403
    if (*s == c)
 
404
      return (char *) s;
 
405
 
 
406
  return 0;
 
407
}
 
408
 
 
409
 
316
410
static inline unsigned int
317
411
grub_abs (int x)
318
412
{
322
416
    return (unsigned int) x;
323
417
}
324
418
 
325
 
static inline long
326
 
grub_min (long x, long y)
327
 
{
328
 
  if (x < y)
329
 
    return x;
330
 
  else
331
 
    return y;
332
 
}
333
 
 
334
 
static inline long
335
 
grub_max (long x, long y)
336
 
{
337
 
  if (x > y)
338
 
    return x;
339
 
  else
340
 
    return y;
341
 
}
342
 
 
343
419
/* Rounded-up division */
344
420
static inline unsigned int
345
421
grub_div_roundup (unsigned int x, unsigned int y)
348
424
}
349
425
 
350
426
/* Reboot the machine.  */
351
 
void EXPORT_FUNC (grub_reboot) (void) __attribute__ ((noreturn));
 
427
#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
 
428
void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
 
429
#else
 
430
void grub_reboot (void) __attribute__ ((noreturn));
 
431
#endif
352
432
 
353
433
#ifdef GRUB_MACHINE_PCBIOS
354
434
/* Halt the system, using APM if possible. If NO_APM is true, don't
355
435
 * use APM even if it is available.  */
356
436
void grub_halt (int no_apm) __attribute__ ((noreturn));
 
437
#elif defined (__mips__)
 
438
void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
357
439
#else
358
440
void grub_halt (void) __attribute__ ((noreturn));
359
441
#endif
365
447
#define grub_no_autoload 0
366
448
#endif
367
449
 
 
450
static inline void
 
451
grub_error_save (struct grub_error_saved *save)
 
452
{
 
453
  grub_memcpy (save->errmsg, grub_errmsg, sizeof (save->errmsg));
 
454
  save->grub_errno = grub_errno;
 
455
  grub_errno = GRUB_ERR_NONE;
 
456
}
 
457
 
 
458
static inline void
 
459
grub_error_load (const struct grub_error_saved *save)
 
460
{
 
461
  grub_memcpy (grub_errmsg, save->errmsg, sizeof (grub_errmsg));
 
462
  grub_errno = save->grub_errno;
 
463
}
 
464
 
368
465
#endif /* ! GRUB_MISC_HEADER */