Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
Signed-off-by: leleliu008 <[email protected]>
  • Loading branch information
leleliu008 committed Apr 28, 2024
1 parent 3ecfc2c commit c77f25a
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 185 deletions.
32 changes: 24 additions & 8 deletions algorithm/base16/src/main/base16-decode.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <base16.h>

#define COLOR_GREEN "\033[0;32m"
Expand Down Expand Up @@ -49,37 +51,44 @@ static void showHelp() {

int main(int argc, char *argv[]) {
if (argc < 2) {
char inputBuff[1024];
char readBuf[1024];

for (;;) {
ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1024);
ssize_t readSize = read(STDIN_FILENO, readBuf, 1024);

if (inputSize < 0) {
if (readSize == -1) {
perror(NULL);
return 1;
}

if (inputSize == 0) {
if (readSize == 0) {
if (isatty(STDOUT_FILENO)) {
printf("\n");
}

return 0;
}

size_t outputSize = inputSize >> 1;
size_t outputSize = readSize >> 1;
unsigned char outputBuff[outputSize];

int ret = base16_decode(outputBuff, inputBuff, inputSize);
int ret = base16_decode(outputBuff, readBuf, readSize);

if (ret != 0) {
return ret;
}

if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) {
ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize);

if (writtenSize == -1) {
perror(NULL);
return 1;
}

if ((size_t)writtenSize != outputSize) {
fprintf(stderr, "not fully written to stdout.\n");
return 1;
}
}
}

Expand Down Expand Up @@ -108,11 +117,18 @@ int main(int argc, char *argv[]) {
return ret;
}

if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) {
ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize);

if (writtenSize == -1) {
perror(NULL);
return 1;
}

if ((size_t)writtenSize != outputSize) {
fprintf(stderr, "not fully written to stdout.\n");
return 1;
}

if (isatty(STDOUT_FILENO)) {
printf("\n");
}
Expand Down
32 changes: 24 additions & 8 deletions algorithm/base16/src/main/base16-encode.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <base16.h>

#define COLOR_GREEN "\033[0;32m"
Expand Down Expand Up @@ -49,37 +51,44 @@ static void showHelp() {

int main(int argc, char *argv[]) {
if (argc < 2) {
unsigned char inputBuff[1024];
unsigned char readBuf[1024];

for (;;) {
ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1024);
ssize_t readSize = read(STDIN_FILENO, readBuf, 1024);

if (inputSize < 0) {
if (readSize == -1) {
perror(NULL);
return 1;
}

if (inputSize == 0) {
if (readSize == 0) {
if (isatty(STDOUT_FILENO)) {
printf("\n");
}

return 0;
}

size_t outputSize = inputSize << 1;
size_t outputSize = readSize << 1;
char outputBuff[outputSize];

int ret = base16_encode(outputBuff, inputBuff, inputSize, true);
int ret = base16_encode(outputBuff, readBuf, readSize, true);

if (ret != 0) {
return ret;
}

if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) {
ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize);

if (writtenSize == -1) {
perror(NULL);
return 1;
}

if ((size_t)writtenSize != outputSize) {
fprintf(stderr, "not fully written to stdout.\n");
return 1;
}
}
}

Expand Down Expand Up @@ -108,11 +117,18 @@ int main(int argc, char *argv[]) {
return ret;
}

if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) {
ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize);

if (writtenSize == -1) {
perror(NULL);
return 1;
}

if ((size_t)writtenSize != outputSize) {
fprintf(stderr, "not fully written to stdout.\n");
return 1;
}

if (isatty(STDOUT_FILENO)) {
printf("\n");
}
Expand Down
11 changes: 0 additions & 11 deletions algorithm/bcc/libbcc.pc

This file was deleted.

11 changes: 0 additions & 11 deletions algorithm/chinese-calendar/libchinese-calendar.pc

This file was deleted.

11 changes: 0 additions & 11 deletions algorithm/url/liburl.pc

This file was deleted.

4 changes: 3 additions & 1 deletion library/libgit2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_executable(git-remote-list git-remote-list.c)
add_executable(git-branch-list git-branch-list.c)
add_executable(git-config-list git-config-list.c)
add_executable(git-tag-name-list git-tag-name-list.c)
add_executable(git-submodule-update git-submodule-update.c)

target_link_libraries(git-open LIBGIT2::LIBGIT2)
target_link_libraries(git-init LIBGIT2::LIBGIT2)
Expand All @@ -42,9 +43,10 @@ target_link_libraries(git-remote-list LIBGIT2::LIBGIT2)
target_link_libraries(git-branch-list LIBGIT2::LIBGIT2)
target_link_libraries(git-config-list LIBGIT2::LIBGIT2)
target_link_libraries(git-tag-name-list LIBGIT2::LIBGIT2)
target_link_libraries(git-submodule-update LIBGIT2::LIBGIT2)

########################################################################################

include(GNUInstallDirs)

install(TARGETS git-open git-init git-pull git-sync git-fetch git-clone git-rev-parse git-clone-simple git-status-list git-remote-list git-branch-list git-config-list git-tag-name-list RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS git-open git-init git-pull git-sync git-fetch git-clone git-rev-parse git-clone-simple git-status-list git-remote-list git-branch-list git-config-list git-tag-name-list git-submodule-update RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
76 changes: 76 additions & 0 deletions library/libgit2/git-submodule-update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include <unistd.h>

#include <git2.h>

static int git_submodule_foreach_callback(git_submodule * submodule, const char * name, void * payload) {
unsigned int status = 0;

int ret = git_submodule_status(&status, (git_repository*)payload, name, GIT_SUBMODULE_IGNORE_UNTRACKED);

if (ret != GIT_OK) {
const git_error * error = git_error_last();
fprintf(stderr, "%s\n", error->message);
return ret;
}

printf("status of (%s):%u\n", name, status);

return git_submodule_update(submodule, true, NULL);
}

int do_git_submodule_update_init(const char * gitRepoPath) {
git_libgit2_init();

git_repository * gitRepo = NULL;

// https://libgit2.org/libgit2/#HEAD/group/repository/git_repository_init
int ret = git_repository_init(&gitRepo, gitRepoPath, false);

if (ret != GIT_OK) {
const git_error * error = git_error_last();
fprintf(stderr, "%s\n", error->message);
}

ret = git_submodule_foreach(gitRepo, git_submodule_foreach_callback, gitRepo);

if (ret != GIT_OK) {
const git_error * error = git_error_last();
fprintf(stderr, "%s\n", error->message);
}

git_repository_free(gitRepo);

git_libgit2_shutdown();

return ret;
}

int main(int argc, char* argv[]) {
if (argc == 1) {
printf("%s [ -C <WORKING-DIR> ]\n", argv[0]);
return 0;
}

const char * workingDirPath = ".";

for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-C") == 0) {
i++;
workingDirPath = argv[i];

if (workingDirPath == NULL) {
fprintf(stderr, "-C option should have non-empty value.");
return 1;
}
} else {
fprintf(stderr, "unrecognized option: %s", argv[i]);
return 2;
}
}

return do_git_submodule_update_init(workingDirPath);
}
11 changes: 0 additions & 11 deletions library/mbedTLS/base64/base64.pc

This file was deleted.

4 changes: 2 additions & 2 deletions library/mbedTLS/base64/src/include/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* inputBuffer 要编码的字节的起始指针
* inputBufferSizeInBytes 要编码的字节的长度
*/
int base64_encode(char* outputBuffer, size_t outputBufferSizeInBytes, unsigned char* inputBuffer, size_t inputBufferSizeInBytes);
int base64_encode(char * outputBuffer, size_t outputBufferSizeInBytes, unsigned char * inputBuffer, size_t inputBufferSizeInBytes);

/* base64解码
* outputBuffer base64解码后的字符串存放缓存区
* outputBufferSizeInBytes base64解码后的字符串存放缓存区的大小
* writenToOutputBufferSizeInBytes 真实的向缓存区写入了多少字节
* input base64编码的字符串
*/
int base64_decode(unsigned char* outputBuffer, size_t outputBufferSizeInBytes, size_t* writenToOutputBufferSizeInBytes, char* input);
int base64_decode(unsigned char * outputBuffer, size_t outputBufferSizeInBytes, size_t * writenToOutputBufferSizeInBytes, char * inputBuffer, size_t inputBufferSizeInBytes);

#ifdef __cplusplus
}
Expand Down
30 changes: 7 additions & 23 deletions library/mbedTLS/base64/src/lib/base64.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
// http://blog.fpliu.com/it/software/mbedTLS/include/base64.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <base64.h>

#include "base64_mbedtls.h"

size_t get_base64_encode_output_length_in_bytes(size_t inputBufferSizeInBytes) {
//看看需要分配多少个字节
//编码后,长度变为原来的4/3倍
size_t n = inputBufferSizeInBytes / 3U + (((inputBufferSizeInBytes % 3U) == 0) ? 0U : 1U);
return (n << 2);
}

int base64_encode(char* outputBuffer, size_t outputBufferSizeInBytes, unsigned char* inputBuffer, size_t inputBufferSizeInBytes) {
size_t writtenToOutputBufferCountInBytes = 0U;
if (0 == mbedtls_base64_encode((unsigned char*)outputBuffer, outputBufferSizeInBytes, &writtenToOutputBufferCountInBytes, inputBuffer, inputBufferSizeInBytes)) {
return 0;
} else {
return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
}
int base64_encode(char * outputBuffer, size_t outputBufferSizeInBytes, unsigned char * inputBuffer, size_t inputBufferSizeInBytes) {
size_t writtenSizeInBytes;
return mbedtls_base64_encode((unsigned char*)outputBuffer, outputBufferSizeInBytes, &writtenSizeInBytes, inputBuffer, inputBufferSizeInBytes);
}

int base64_decode(unsigned char* outputBuffer, size_t outputBufferSizeInBytes, size_t* writtenToOutputBufferCountInBytes, char* input) {
size_t inputBufferSizeInBytes = strlen(input);

//base64编码的字符串的长度必须是4的整数倍
if ((inputBufferSizeInBytes & 3U) != 0U) {
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}

if (0 == mbedtls_base64_decode(outputBuffer, outputBufferSizeInBytes, writtenToOutputBufferCountInBytes, (unsigned char *)input, inputBufferSizeInBytes)) {
return 0;
} else {
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}
int base64_decode(unsigned char * outputBuffer, size_t outputBufferSizeInBytes, size_t * writtenSizeInBytes, char * inputBuffer, size_t inputBufferSizeInBytes) {
return mbedtls_base64_decode(outputBuffer, outputBufferSizeInBytes, writtenSizeInBytes, (unsigned char *)inputBuffer, inputBufferSizeInBytes);
}
Loading

0 comments on commit c77f25a

Please sign in to comment.