]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/BsdSocketLib/base64.c
Get rid of some rcsid blocks. The EDK II build options cause a build break on the...
[mirror_edk2.git] / StdLib / BsdSocketLib / base64.c
index 593360535c8340c0b0a0755c009f640fa8a4e5c9..d8c588d19dd2964b16aa128baa53b13dae065b9e 100644 (file)
  * Portions copyright (c) 1999, 2000\r
  * Intel Corporation.\r
  * All rights reserved.\r
- * \r
+ *\r
  * Redistribution and use in source and binary forms, with or without\r
  * modification, are permitted provided that the following conditions\r
  * are met:\r
- * \r
+ *\r
  * 1. Redistributions of source code must retain the above copyright\r
  *    notice, this list of conditions and the following disclaimer.\r
- * \r
+ *\r
  * 2. Redistributions in binary form must reproduce the above copyright\r
  *    notice, this list of conditions and the following disclaimer in the\r
  *    documentation and/or other materials provided with the distribution.\r
- * \r
+ *\r
  * 3. All advertising materials mentioning features or use of this software\r
  *    must display the following acknowledgement:\r
- * \r
+ *\r
  *    This product includes software developed by Intel Corporation and\r
  *    its contributors.\r
- * \r
+ *\r
  * 4. Neither the name of Intel Corporation or its contributors may be\r
  *    used to endorse or promote products derived from this software\r
  *    without specific prior written permission.\r
- * \r
+ *\r
  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r
  * THE POSSIBILITY OF SUCH DAMAGE.\r
- * \r
+ *\r
+  base64.c,v 1.1.1.1 2003/11/19 01:51:25 kyu3 Exp\r
  */\r
 \r
-#if !defined(LINT) && !defined(CODECENTER)\r
-static char rcsid[] = "$Id: base64.c,v 1.1.1.1 2003/11/19 01:51:25 kyu3 Exp $";\r
-#endif /* not lint */\r
-\r
 #include <sys/types.h>\r
 #include <sys/param.h>\r
 #include <sys/socket.h>\r
@@ -101,7 +98,7 @@ static char rcsid[] = "$Id: base64.c,v 1.1.1.1 2003/11/19 01:51:25 kyu3 Exp $";
 #define Assert(Cond) if (!(Cond)) abort()\r
 \r
 static const char Base64[] =\r
-       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
 static const char Pad64 = '=';\r
 \r
 /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)\r
@@ -152,79 +149,79 @@ static const char Pad64 = '=';
    end of the data is performed using the '=' character.\r
 \r
    Since all base64 input is an integral number of octets, only the\r
-         -------------------------------------------------                       \r
+         -------------------------------------------------\r
    following cases can arise:\r
-   \r
+\r
        (1) the final quantum of encoding input is an integral\r
            multiple of 24 bits; here, the final unit of encoded\r
-          output will be an integral multiple of 4 characters\r
-          with no "=" padding,\r
+     output will be an integral multiple of 4 characters\r
+     with no "=" padding,\r
        (2) the final quantum of encoding input is exactly 8 bits;\r
            here, the final unit of encoded output will be two\r
-          characters followed by two "=" padding characters, or\r
+     characters followed by two "=" padding characters, or\r
        (3) the final quantum of encoding input is exactly 16 bits;\r
            here, the final unit of encoded output will be three\r
-          characters followed by one "=" padding character.\r
+     characters followed by one "=" padding character.\r
    */\r
 \r
 int\r
 b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {\r
-       size_t datalength = 0;\r
-       u_char input[3];\r
-       u_char output[4];\r
-       size_t i;\r
-\r
-       while (2 < srclength) {\r
-               input[0] = *src++;\r
-               input[1] = *src++;\r
-               input[2] = *src++;\r
-               srclength -= 3;\r
-\r
-               output[0] = input[0] >> 2;\r
-               output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
-               output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
-               output[3] = input[2] & 0x3f;\r
-               Assert(output[0] < 64);\r
-               Assert(output[1] < 64);\r
-               Assert(output[2] < 64);\r
-               Assert(output[3] < 64);\r
-\r
-               if (datalength + 4 > targsize)\r
-                       return (-1);\r
-               target[datalength++] = Base64[output[0]];\r
-               target[datalength++] = Base64[output[1]];\r
-               target[datalength++] = Base64[output[2]];\r
-               target[datalength++] = Base64[output[3]];\r
-       }\r
-    \r
-       /* Now we worry about padding. */\r
-       if (0 != srclength) {\r
-               /* Get what's left. */\r
-               input[0] = input[1] = input[2] = '\0';\r
-               for (i = 0; i < srclength; i++)\r
-                       input[i] = *src++;\r
-       \r
-               output[0] = input[0] >> 2;\r
-               output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
-               output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
-               Assert(output[0] < 64);\r
-               Assert(output[1] < 64);\r
-               Assert(output[2] < 64);\r
-\r
-               if (datalength + 4 > targsize)\r
-                       return (-1);\r
-               target[datalength++] = Base64[output[0]];\r
-               target[datalength++] = Base64[output[1]];\r
-               if (srclength == 1)\r
-                       target[datalength++] = Pad64;\r
-               else\r
-                       target[datalength++] = Base64[output[2]];\r
-               target[datalength++] = Pad64;\r
-       }\r
-       if (datalength >= targsize)\r
-               return (-1);\r
-       target[datalength] = '\0';      /* Returned value doesn't count \0. */\r
-       return ((int)datalength);\r
+  size_t datalength = 0;\r
+  u_char input[3];\r
+  u_char output[4];\r
+  size_t i;\r
+\r
+  while (2 < srclength) {\r
+    input[0] = *src++;\r
+    input[1] = *src++;\r
+    input[2] = *src++;\r
+    srclength -= 3;\r
+\r
+    output[0] = input[0] >> 2;\r
+    output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
+    output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
+    output[3] = input[2] & 0x3f;\r
+    Assert(output[0] < 64);\r
+    Assert(output[1] < 64);\r
+    Assert(output[2] < 64);\r
+    Assert(output[3] < 64);\r
+\r
+    if (datalength + 4 > targsize)\r
+      return (-1);\r
+    target[datalength++] = Base64[output[0]];\r
+    target[datalength++] = Base64[output[1]];\r
+    target[datalength++] = Base64[output[2]];\r
+    target[datalength++] = Base64[output[3]];\r
+  }\r
+\r
+  /* Now we worry about padding. */\r
+  if (0 != srclength) {\r
+    /* Get what's left. */\r
+    input[0] = input[1] = input[2] = '\0';\r
+    for (i = 0; i < srclength; i++)\r
+      input[i] = *src++;\r
+\r
+    output[0] = input[0] >> 2;\r
+    output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
+    output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
+    Assert(output[0] < 64);\r
+    Assert(output[1] < 64);\r
+    Assert(output[2] < 64);\r
+\r
+    if (datalength + 4 > targsize)\r
+      return (-1);\r
+    target[datalength++] = Base64[output[0]];\r
+    target[datalength++] = Base64[output[1]];\r
+    if (srclength == 1)\r
+      target[datalength++] = Pad64;\r
+    else\r
+      target[datalength++] = Base64[output[2]];\r
+    target[datalength++] = Pad64;\r
+  }\r
+  if (datalength >= targsize)\r
+    return (-1);\r
+  target[datalength] = '\0';  /* Returned value doesn't count \0. */\r
+  return ((int)datalength);\r
 }\r
 \r
 /* skips all whitespace anywhere.\r
@@ -235,123 +232,123 @@ b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
 \r
 int\r
 b64_pton(\r
-       char const *src,\r
-       u_char *target,\r
-       size_t targsize\r
-       )\r
+  char const *src,\r
+  u_char *target,\r
+  size_t targsize\r
+  )\r
 {\r
-       int tarindex, state, ch;\r
-       char *pos;\r
-\r
-       state = 0;\r
-       tarindex = 0;\r
-\r
-       while ((ch = *src++) != '\0') {\r
-               if (isspace(ch))        /* Skip whitespace anywhere. */\r
-                       continue;\r
-\r
-               if (ch == Pad64)\r
-                       break;\r
-\r
-               pos = strchr(Base64, ch);\r
-               if (pos == 0)           /* A non-base64 character. */\r
-                       return (-1);\r
-\r
-               switch (state) {\r
-               case 0:\r
-                       if (target) {\r
-                               if ((size_t)tarindex >= targsize)\r
-                                       return (-1);\r
-                               target[tarindex] = (u_char)((pos - Base64) << 2);\r
-                       }\r
-                       state = 1;\r
-                       break;\r
-               case 1:\r
-                       if (target) {\r
-                               if ((size_t)tarindex + 1 >= targsize)\r
-                                       return (-1);\r
-                               target[tarindex]   |= (u_char)((pos - Base64) >> 4);\r
-                               target[tarindex+1]  = (u_char)(((pos - Base64) & 0x0f)\r
-                                                       << 4) ;\r
-                       }\r
-                       tarindex++;\r
-                       state = 2;\r
-                       break;\r
-               case 2:\r
-                       if (target) {\r
-                               if ((size_t)tarindex + 1 >= targsize)\r
-                                       return (-1);\r
-                               target[tarindex]   |= (u_char)((pos - Base64) >> 2);\r
-                               target[tarindex+1]  = (u_char)(((pos - Base64) & 0x03)\r
-                                                       << 6);\r
-                       }\r
-                       tarindex++;\r
-                       state = 3;\r
-                       break;\r
-               case 3:\r
-                       if (target) {\r
-                               if ((size_t)tarindex >= targsize)\r
-                                       return (-1);\r
-                               target[tarindex] |= (u_char)(pos - Base64);\r
-                       }\r
-                       tarindex++;\r
-                       state = 0;\r
-                       break;\r
-               default:\r
-                       abort();\r
-               }\r
-       }\r
-\r
-       /*\r
-        * We are done decoding Base-64 chars.  Let's see if we ended\r
-        * on a byte boundary, and/or with erroneous trailing characters.\r
-        */\r
-\r
-       if (ch == Pad64) {              /* We got a pad char. */\r
-               ch = *src++;            /* Skip it, get next. */\r
-               switch (state) {\r
-               case 0:         /* Invalid = in first position */\r
-               case 1:         /* Invalid = in second position */\r
-                       return (-1);\r
-\r
-               case 2:         /* Valid, means one byte of info */\r
-                       /* Skip any number of spaces. */\r
-                       for ((void)NULL; ch != '\0'; ch = *src++)\r
-                               if (!isspace(ch))\r
-                                       break;\r
-                       /* Make sure there is another trailing = sign. */\r
-                       if (ch != Pad64)\r
-                               return (-1);\r
-                       ch = *src++;            /* Skip the = */\r
-                       /* Fall through to "single trailing =" case. */\r
-                       /* FALLTHROUGH */\r
-\r
-               case 3:         /* Valid, means two bytes of info */\r
-                       /*\r
-                        * We know this char is an =.  Is there anything but\r
-                        * whitespace after it?\r
-                        */\r
-                       for ((void)NULL; ch != '\0'; ch = *src++)\r
-                               if (!isspace(ch))\r
-                                       return (-1);\r
-\r
-                       /*\r
-                        * Now make sure for cases 2 and 3 that the "extra"\r
-                        * bits that slopped past the last full byte were\r
-                        * zeros.  If we don't check them, they become a\r
-                        * subliminal channel.\r
-                        */\r
-                       if (target && target[tarindex] != 0)\r
-                               return (-1);\r
-               }\r
-       } else {\r
-               /*\r
-                * We ended by seeing the end of the string.  Make sure we\r
-                * have no partial bytes lying around.\r
-                */\r
-               if (state != 0)\r
-                       return (-1);\r
-       }\r
-\r
-       return (tarindex);\r
+  int tarindex, state, ch;\r
+  char *pos;\r
+\r
+  state = 0;\r
+  tarindex = 0;\r
+\r
+  while ((ch = *src++) != '\0') {\r
+    if (isspace(ch))  /* Skip whitespace anywhere. */\r
+      continue;\r
+\r
+    if (ch == Pad64)\r
+      break;\r
+\r
+    pos = strchr(Base64, ch);\r
+    if (pos == 0)     /* A non-base64 character. */\r
+      return (-1);\r
+\r
+    switch (state) {\r
+    case 0:\r
+      if (target) {\r
+        if ((size_t)tarindex >= targsize)\r
+          return (-1);\r
+        target[tarindex] = (u_char)((pos - Base64) << 2);\r
+      }\r
+      state = 1;\r
+      break;\r
+    case 1:\r
+      if (target) {\r
+        if ((size_t)tarindex + 1 >= targsize)\r
+          return (-1);\r
+        target[tarindex]   |= (u_char)((pos - Base64) >> 4);\r
+        target[tarindex+1]  = (u_char)(((pos - Base64) & 0x0f)\r
+              << 4) ;\r
+      }\r
+      tarindex++;\r
+      state = 2;\r
+      break;\r
+    case 2:\r
+      if (target) {\r
+        if ((size_t)tarindex + 1 >= targsize)\r
+          return (-1);\r
+        target[tarindex]   |= (u_char)((pos - Base64) >> 2);\r
+        target[tarindex+1]  = (u_char)(((pos - Base64) & 0x03)\r
+              << 6);\r
+      }\r
+      tarindex++;\r
+      state = 3;\r
+      break;\r
+    case 3:\r
+      if (target) {\r
+        if ((size_t)tarindex >= targsize)\r
+          return (-1);\r
+        target[tarindex] |= (u_char)(pos - Base64);\r
+      }\r
+      tarindex++;\r
+      state = 0;\r
+      break;\r
+    default:\r
+      abort();\r
+    }\r
+  }\r
+\r
+  /*\r
+   * We are done decoding Base-64 chars.  Let's see if we ended\r
+   * on a byte boundary, and/or with erroneous trailing characters.\r
+   */\r
+\r
+  if (ch == Pad64) {    /* We got a pad char. */\r
+    ch = *src++;    /* Skip it, get next. */\r
+    switch (state) {\r
+    case 0:   /* Invalid = in first position */\r
+    case 1:   /* Invalid = in second position */\r
+      return (-1);\r
+\r
+    case 2:   /* Valid, means one byte of info */\r
+      /* Skip any number of spaces. */\r
+      for ((void)NULL; ch != '\0'; ch = *src++)\r
+        if (!isspace(ch))\r
+          break;\r
+      /* Make sure there is another trailing = sign. */\r
+      if (ch != Pad64)\r
+        return (-1);\r
+      ch = *src++;    /* Skip the = */\r
+      /* Fall through to "single trailing =" case. */\r
+      /* FALLTHROUGH */\r
+\r
+    case 3:   /* Valid, means two bytes of info */\r
+      /*\r
+       * We know this char is an =.  Is there anything but\r
+       * whitespace after it?\r
+       */\r
+      for ((void)NULL; ch != '\0'; ch = *src++)\r
+        if (!isspace(ch))\r
+          return (-1);\r
+\r
+      /*\r
+       * Now make sure for cases 2 and 3 that the "extra"\r
+       * bits that slopped past the last full byte were\r
+       * zeros.  If we don't check them, they become a\r
+       * subliminal channel.\r
+       */\r
+      if (target && target[tarindex] != 0)\r
+        return (-1);\r
+    }\r
+  } else {\r
+    /*\r
+     * We ended by seeing the end of the string.  Make sure we\r
+     * have no partial bytes lying around.\r
+     */\r
+    if (state != 0)\r
+      return (-1);\r
+  }\r
+\r
+  return (tarindex);\r
 }\r