From ad8d1db68f8e6946a7d93d206f9e434663386607 Mon Sep 17 00:00:00 2001 From: tradke Date: Wed, 30 Aug 2006 08:49:11 +0000 Subject: Properly encode/decode non-printable characters as 3-digit octal numbers in a parameter string to be checkpointed/recovered. Thanks to Erik Schnetter for this patch. git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOUtil/trunk@247 b32723a9-ab3a-4a60-88e2-2e5d99d7c17a --- src/CheckpointRecovery.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/CheckpointRecovery.c b/src/CheckpointRecovery.c index 9d6ab65..ba32fb7 100644 --- a/src/CheckpointRecovery.c +++ b/src/CheckpointRecovery.c @@ -65,6 +65,7 @@ typedef int (*recover_fn_t) (cGH *GH, const char *basefilename,int called_from); ********************************************************************/ static char *EncodeString (const char *string); static char *DecodeString (const char *string); +static int OctalDigitToInt (const char digit); static void SetInputFlag (int vindex, const char *optstring, void *arg); #ifdef HAVE_DIRENT_H static int CompareFiles (const void *a, const void *b); @@ -974,7 +975,7 @@ static char *EncodeString (const char *string) { if (! isprint (string[i])) { - sprintf (p, "\\%.03d", (unsigned char)string[i]); + sprintf (p, "\\%.03o", (unsigned char)string[i]); p += 4; } else @@ -997,6 +998,7 @@ static char *DecodeString (const char *string) { size_t i, len = strlen (string); char *p, *retval; + int o1, o2, o3; p = retval = malloc (len + 1); @@ -1021,12 +1023,11 @@ static char *DecodeString (const char *string) else { if (i+3 >= len) goto bail_out; - if (! isdigit (string[i+1]) || - ! isdigit (string[i+2]) || - ! isdigit (string[i+3])) goto bail_out; - *p++ = (char) (100*(string[i+1] - '0') + - 10*(string[i+2] - '0') + - 1*(string[i+3] - '0')); + o1 = OctalDigitToInt (string[i+1]); + o2 = OctalDigitToInt (string[i+2]); + o3 = OctalDigitToInt (string[i+3]); + if (o1 < 0 || o2 < 0 || o3 < 0) goto bail_out; + *p++ = (char) (8*8*o1 + 8*o2 + o3); i += 3; } } @@ -1109,6 +1110,27 @@ static void SetInputFlag (int vindex, const char *optstring, void *flags) } +/* Convert an octal digit to an integer. Return -1 if this is not an + octal digit. Note that this routine does not assume ASCII encoding + (and thus cannot calculate digit-'0'), and does not use + non-standard library function such as isoctal. */ +static int OctalDigitToInt (const char digit) +{ + switch (digit) + { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + } + return -1; +} + + #ifdef HAVE_DIRENT_H /* callback for qsort() to sort the list of recovery files found */ static int CompareFiles (const void *a, const void *b) -- cgit v1.2.3