447 |
} |
} |
448 |
} |
} |
449 |
} |
} |
450 |
while (number > 0); |
while (number > 0 || (total && line[total - 1] != '\n')); |
451 |
/* syslog (LOG_INFO, "readline: %s", line); */ |
/* syslog (LOG_INFO, "readline: %s", line); */ |
452 |
return line; |
return line; |
453 |
} |
} |
454 |
|
|
455 |
|
char * |
456 |
|
imap4d_readline_ex (FILE *fp) |
457 |
|
{ |
458 |
|
int len; |
459 |
|
char *s = imap4d_readline (fp); |
460 |
|
|
461 |
|
if (s && (len = strlen (s)) > 0 && s[len-1] == '\n') |
462 |
|
s[len-1] = 0; |
463 |
|
return s; |
464 |
|
} |
465 |
|
|
466 |
int |
int |
467 |
util_do_command (char *prompt) |
util_do_command (char *prompt) |
468 |
{ |
{ |
768 |
*save = items; |
*save = items; |
769 |
return rc; |
return rc; |
770 |
} |
} |
771 |
|
|
772 |
|
int |
773 |
|
util_base64_encode (const unsigned char *input, size_t input_len, |
774 |
|
unsigned char **output, size_t *output_len) |
775 |
|
{ |
776 |
|
static char b64tab[] = |
777 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
778 |
|
size_t olen = 4 * (input_len + 2) / 3; |
779 |
|
unsigned char *out = malloc (olen); |
780 |
|
|
781 |
|
if (!out) |
782 |
|
return 1; |
783 |
|
*output = out; |
784 |
|
while (input_len >= 3) |
785 |
|
{ |
786 |
|
*out++ = b64tab[input[0] >> 2]; |
787 |
|
*out++ = b64tab[((input[0] << 4) & 0x30) | (input[1] >> 4)]; |
788 |
|
*out++ = b64tab[((input[1] << 2) & 0x3c) | (input[2] >> 6)]; |
789 |
|
*out++ = b64tab[input[2] & 0x3f]; |
790 |
|
olen -= 4; |
791 |
|
input_len -= 3; |
792 |
|
input += 3; |
793 |
|
} |
794 |
|
|
795 |
|
if (input_len > 0) |
796 |
|
{ |
797 |
|
unsigned char c = (input[0] << 4) & 0x30; |
798 |
|
*out++ = b64tab[input[0] >> 2]; |
799 |
|
if (input_len > 0) |
800 |
|
c |= input[1] >> 4; |
801 |
|
*out++ = b64tab[c]; |
802 |
|
*out++ = (input_len < 2) ? '-' : b64tab[(input[1] << 2) & 0x3c]; |
803 |
|
*out++ = '='; |
804 |
|
} |
805 |
|
*output_len = out - *output; |
806 |
|
return 0; |
807 |
|
} |
808 |
|
|
809 |
|
int |
810 |
|
util_base64_decode (const unsigned char *input, size_t input_len, |
811 |
|
unsigned char **output, size_t *output_len) |
812 |
|
{ |
813 |
|
static int b64val[128] = { |
814 |
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
815 |
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
816 |
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
817 |
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
818 |
|
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
819 |
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
820 |
|
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
821 |
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 |
822 |
|
}; |
823 |
|
int olen = input_len; |
824 |
|
unsigned char *out = malloc (olen); |
825 |
|
|
826 |
|
if (!out) |
827 |
|
return 1; |
828 |
|
*output = out; |
829 |
|
do |
830 |
|
{ |
831 |
|
if (input[0] > 127 || b64val[input[0]] == -1 |
832 |
|
|| input[1] > 127 || b64val[input[1]] == -1 |
833 |
|
|| input[2] > 127 || ((input[2] != '=') && (b64val[input[2]] == -1)) |
834 |
|
|| input[3] > 127 || ((input[3] != '=') && (b64val[input[3]] == -1))) |
835 |
|
return -1; |
836 |
|
*out++ = (b64val[input[0]] << 2) | (b64val[input[1]] >> 4); |
837 |
|
if (input[2] != '=') |
838 |
|
{ |
839 |
|
*out++ = ((b64val[input[1]] << 4) & 0xf0) | (b64val[input[2]] >> 2); |
840 |
|
if (input[3] != '=') |
841 |
|
*out++ = ((b64val[input[2]] << 6) & 0xc0) | b64val[input[3]]; |
842 |
|
} |
843 |
|
input += 4; |
844 |
|
input_len -= 4; |
845 |
|
} |
846 |
|
while (input_len > 0); |
847 |
|
*output_len = out - *output; |
848 |
|
return 0; |
849 |
|
} |
850 |
|
|
851 |
|
char * |
852 |
|
util_localname () |
853 |
|
{ |
854 |
|
static char *localname; |
855 |
|
|
856 |
|
if (!localname) |
857 |
|
{ |
858 |
|
char *name; |
859 |
|
int name_len = 256; |
860 |
|
int status; |
861 |
|
|
862 |
|
name = malloc (name_len); |
863 |
|
while (name |
864 |
|
&& (status = gethostname (name, name_len)) == 0 |
865 |
|
&& !memchr (name, 0, name_len)) |
866 |
|
{ |
867 |
|
name_len *= 2; |
868 |
|
name = realloc (name, name_len); |
869 |
|
} |
870 |
|
if (status) |
871 |
|
{ |
872 |
|
syslog (LOG_CRIT, "Can't find out my own hostname"); |
873 |
|
exit (1); |
874 |
|
} |
875 |
|
|
876 |
|
localname = name; |
877 |
|
} |
878 |
|
return localname; |
879 |
|
} |