Base64.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <inttypes.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <lib/core/CHIPCore.h>
  23. #include <lib/shell/Commands.h>
  24. #include <lib/shell/Engine.h>
  25. #include <lib/shell/commands/Help.h>
  26. #include <lib/support/Base64.h>
  27. #include <lib/support/CHIPArgParser.hpp>
  28. #include <lib/support/CodeUtils.h>
  29. #include <lib/support/RandUtils.h>
  30. chip::Shell::Engine sShellBase64Commands;
  31. namespace chip {
  32. namespace Shell {
  33. static int Base64HelpHandler(int argc, char ** argv)
  34. {
  35. sShellBase64Commands.ForEachCommand(PrintCommandHelp, nullptr);
  36. return 0;
  37. }
  38. static int Base64DecodeHandler(int argc, char ** argv)
  39. {
  40. streamer_t * sout = streamer_get();
  41. uint32_t binarySize;
  42. uint8_t binary[256];
  43. VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT);
  44. binarySize = Base64Decode(argv[0], strlen(argv[0]), binary);
  45. streamer_print_hex(sout, binary, binarySize);
  46. streamer_printf(sout, "\r\n");
  47. return CHIP_NO_ERROR;
  48. }
  49. static int Base64EncodeHandler(int argc, char ** argv)
  50. {
  51. streamer_t * sout = streamer_get();
  52. char base64[256];
  53. uint8_t binary[256];
  54. uint32_t binarySize, base64Size;
  55. VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT);
  56. ArgParser::ParseHexString(argv[0], strlen(argv[0]), binary, sizeof(binary), binarySize);
  57. base64Size = Base64Encode(binary, binarySize, base64);
  58. streamer_printf(sout, "%.*s\r\n", base64Size, base64);
  59. return CHIP_NO_ERROR;
  60. }
  61. static int Base64Dispatch(int argc, char ** argv)
  62. {
  63. CHIP_ERROR error = CHIP_NO_ERROR;
  64. if (argc == 0)
  65. {
  66. Base64HelpHandler(argc, argv);
  67. return error;
  68. }
  69. error = sShellBase64Commands.ExecCommand(argc, argv);
  70. return error;
  71. }
  72. void RegisterBase64Commands()
  73. {
  74. /// Subcommands for root command: `base64 <subcommand>`
  75. static const shell_command_t sBase64SubCommands[] = {
  76. { &Base64HelpHandler, "help", "Usage: base64 <subcommand>" },
  77. { &Base64EncodeHandler, "encode", "Encode a hex sting as base64. Usage: base64 encode <hex_string>" },
  78. { &Base64DecodeHandler, "decode", "Decode a base64 sting as hex. Usage: base64 decode <base64_string>" },
  79. };
  80. static const shell_command_t sBase64Command = { &Base64Dispatch, "base64", "Base64 encode / decode utilities" };
  81. // Register `base64` subcommands with the local shell dispatcher.
  82. sShellBase64Commands.RegisterCommands(sBase64SubCommands, ArraySize(sBase64SubCommands));
  83. // Register the root `base64` command with the top-level shell.
  84. Engine::Root().RegisterCommands(&sBase64Command, 1);
  85. }
  86. } // namespace Shell
  87. } // namespace chip