~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to kern/misc.c

Tags: upstream-1.98+20100705
ImportĀ upstreamĀ versionĀ 1.98+20100705

Show diffs side-by-side

added added

removed removed

Lines of Context:
143
143
}
144
144
 
145
145
int
146
 
grub_puts (const char *s)
147
 
{
148
 
  while (*s)
149
 
    {
150
 
      grub_putchar (*s);
151
 
      s++;
152
 
    }
153
 
  grub_putchar ('\n');
154
 
 
155
 
  return 1;     /* Cannot fail.  */
156
 
}
157
 
 
158
 
int
159
146
grub_puts_ (const char *s)
160
147
{
161
148
  return grub_puts (_(s));
200
187
    }
201
188
}
202
189
 
 
190
#define PREALLOC_SIZE 255
 
191
 
203
192
int
204
193
grub_vprintf (const char *fmt, va_list args)
205
194
{
206
 
  int ret;
207
 
 
208
 
  ret = grub_vsnprintf_real (0, 0, fmt, args);
209
 
  return ret;
 
195
  grub_size_t s;
 
196
  static char buf[PREALLOC_SIZE + 1];
 
197
  char *curbuf = buf;
 
198
 
 
199
  s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, args);
 
200
  if (s > PREALLOC_SIZE)
 
201
    {
 
202
      curbuf = grub_malloc (s + 1);
 
203
      if (!curbuf)
 
204
        {
 
205
          grub_errno = GRUB_ERR_NONE;
 
206
          buf[PREALLOC_SIZE - 3] = '.';
 
207
          buf[PREALLOC_SIZE - 2] = '.';
 
208
          buf[PREALLOC_SIZE - 1] = '.';
 
209
          buf[PREALLOC_SIZE] = 0;
 
210
        }
 
211
      else
 
212
        s = grub_vsnprintf_real (curbuf, s, fmt, args);
 
213
    }
 
214
 
 
215
  grub_xputs (curbuf);
 
216
 
 
217
  if (curbuf != buf)
 
218
    grub_free (curbuf);
 
219
  
 
220
  return s;
210
221
}
211
222
 
212
223
int
649
660
 
650
661
  void write_char (unsigned char ch)
651
662
    {
652
 
      if (str)
653
 
        {
654
 
          if (count < max_len)
655
 
            *str++ = ch;
656
 
        }
657
 
      else
658
 
        grub_putchar (ch);
 
663
      if (count < max_len)
 
664
        *str++ = ch;
659
665
 
660
666
      count++;
661
667
    }
872
878
        }
873
879
    }
874
880
 
875
 
  if (str)
876
 
    *str = '\0';
 
881
  *str = '\0';
877
882
 
878
883
  return count;
879
884
}
906
911
  return ret;
907
912
}
908
913
 
909
 
#define PREALLOC_SIZE 255
910
 
 
911
914
char *
912
915
grub_xvasprintf (const char *fmt, va_list ap)
913
916
{
942
945
  return ret;
943
946
}
944
947
 
945
 
/* Convert a (possibly null-terminated) UTF-8 string of at most SRCSIZE
946
 
   bytes (if SRCSIZE is -1, it is ignored) in length to a UCS-4 string.
947
 
   Return the number of characters converted. DEST must be able to hold
948
 
   at least DESTSIZE characters.
949
 
   If SRCEND is not NULL, then *SRCEND is set to the next byte after the
950
 
   last byte used in SRC.  */
951
 
grub_size_t
952
 
grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,
953
 
                   const grub_uint8_t *src, grub_size_t srcsize,
954
 
                   const grub_uint8_t **srcend)
955
 
{
956
 
  grub_uint32_t *p = dest;
957
 
  int count = 0;
958
 
  grub_uint32_t code = 0;
959
 
 
960
 
  if (srcend)
961
 
    *srcend = src;
962
 
 
963
 
  while (srcsize && destsize)
964
 
    {
965
 
      grub_uint32_t c = *src++;
966
 
      if (srcsize != (grub_size_t)-1)
967
 
        srcsize--;
968
 
      if (count)
969
 
        {
970
 
          if ((c & 0xc0) != 0x80)
971
 
            {
972
 
              /* invalid */
973
 
              code = '?';
974
 
              /* Character c may be valid, don't eat it.  */
975
 
              src--;
976
 
              if (srcsize != (grub_size_t)-1)
977
 
                srcsize++;
978
 
              count = 0;
979
 
            }
980
 
          else
981
 
            {
982
 
              code <<= 6;
983
 
              code |= (c & 0x3f);
984
 
              count--;
985
 
            }
986
 
        }
987
 
      else
988
 
        {
989
 
          if (c == 0)
990
 
            break;
991
 
 
992
 
          if ((c & 0x80) == 0x00)
993
 
            code = c;
994
 
          else if ((c & 0xe0) == 0xc0)
995
 
            {
996
 
              count = 1;
997
 
              code = c & 0x1f;
998
 
            }
999
 
          else if ((c & 0xf0) == 0xe0)
1000
 
            {
1001
 
              count = 2;
1002
 
              code = c & 0x0f;
1003
 
            }
1004
 
          else if ((c & 0xf8) == 0xf0)
1005
 
            {
1006
 
              count = 3;
1007
 
              code = c & 0x07;
1008
 
            }
1009
 
          else if ((c & 0xfc) == 0xf8)
1010
 
            {
1011
 
              count = 4;
1012
 
              code = c & 0x03;
1013
 
            }
1014
 
          else if ((c & 0xfe) == 0xfc)
1015
 
            {
1016
 
              count = 5;
1017
 
              code = c & 0x01;
1018
 
            }
1019
 
          else
1020
 
            {
1021
 
              /* invalid */
1022
 
              code = '?';
1023
 
              count = 0;
1024
 
            }
1025
 
        }
1026
 
 
1027
 
      if (count == 0)
1028
 
        {
1029
 
          *p++ = code;
1030
 
          destsize--;
1031
 
        }
1032
 
    }
1033
 
 
1034
 
  if (srcend)
1035
 
    *srcend = src;
1036
 
  return p - dest;
1037
 
}
1038
 
 
1039
948
/* Abort GRUB. This function does not return.  */
1040
949
void
1041
950
grub_abort (void)