diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..434b9d5 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +PROG=csum +OBJS=calc_checksum.o + +$(PROG): $(OBJS) + mkdir bin + $(CC) -o $@ $^ + mv $@ bin + +.c.o: + gcc -c $< + +.PHONY: clean +clean: + rm -fr bin $(OBJS) diff --git a/calc_checksum.c b/calc_checksum.c new file mode 100644 index 0000000..a616d66 --- /dev/null +++ b/calc_checksum.c @@ -0,0 +1,141 @@ +#include +#include +#include + +static int ENABLE_DEBUG = 0; +#define DEBUG +#define DEBUG_BEGIN\ + if(ENABLE_DEBUG) do { +#define DEBUG_END\ + } while(0); + +#define NUMS_LEN 100 + +uint8_t calc_sum(uint32_t a, int32_t b, int32_t c, int32_t d, int32_t e); + +// Reserve function +uint8_t calc_sum2(uint32_t a, int32_t* b, int b_len); +uint8_t sum_uint32(uint32_t* a, int len, uint8_t sum); +uint8_t sum_int32(int32_t* a, int len, uint8_t sum); + + +int main(int argc, char *argv[]) { + float nums[NUMS_LEN] = {0.0}; + int i, nums_count = 0; + int num_start = 0; + int num_begin = 0; + + + if (argc <= 1) { + printf("using: %s [--debug | -d] num1,num2,...\n", argv[0]); return 0; + } + if(!strcmp(argv[1], "--debug") || !strcmp(argv[1], "-d")) { + ENABLE_DEBUG = 1, num_begin = 2; + if (argc <= 2) { + printf("using: %s [--debug | -d] num1,num2,...\n", argv[0]); return 0; + } + } + // parse args to nums + for (i = 0; argv[num_begin][i] != '\0'; i++) { + if(argv[num_begin][i] == ',') { + // store + argv[num_begin][i] = '\0'; + nums[nums_count] = (float)atof((const char*)&argv[num_begin][num_start]); + + // update + nums_count++; + num_start = i+1; + } + } + // store + argv[num_begin][i] = '\0'; + nums[nums_count] = (float)atof(&argv[num_begin][num_start]); + + // update + nums_count++; + num_start = i+1; + + // dump + DEBUG_BEGIN + printf("args = ["); + for (i = 0; i < nums_count; i++) { + printf(" %f, ", nums[i]); + } + printf("]\n"); + DEBUG_END + { + uint8_t sum = calc_sum((uint32_t)nums[0],(int32_t)nums[1],(int32_t)nums[2],(int32_t)nums[3],(int32_t)nums[4]); + printf("check sum = %u\n", sum); + } + DEBUG_BEGIN + { + int32_t a[4] = { (int32_t)nums[1],(int32_t)nums[2],(int32_t)nums[3],(int32_t)nums[4] }; + uint8_t sum = calc_sum2(nums[0],a,4); + printf("check2 sum = %u\n", sum); + } + DEBUG_END +} + +uint8_t calc_sum(uint32_t a, int32_t b, int32_t c, int32_t d, int32_t e) +{ + union { + uint32_t u32; + uint8_t u8[4]; + } x; + union { + int32_t u32; + uint8_t u8[4]; + } y; + + uint8_t rv; + DEBUG_BEGIN + printf("a = %u, b = %d, c = %d, d = %d, e = %d\n", a,b,c,d,e); + DEBUG_END + x.u32 =a; + y.u32 =b; + rv= x.u8[0]+x.u8[1]+x.u8[2]+x.u8[3]+y.u8[0]+y.u8[1]+y.u8[2]+y.u8[3]; + y.u32 =c; + rv += y.u8[0]+y.u8[1]+y.u8[2]+y.u8[3]; + y.u32 =d; + rv += y.u8[0]+y.u8[1]+y.u8[2]+y.u8[3]; + y.u32 =e; + rv += y.u8[0]+y.u8[1]+y.u8[2]+y.u8[3]; + return rv; +} + +uint8_t calc_sum2(uint32_t a, int32_t* b, int b_len) +{ + uint8_t rv = 0; + + rv = sum_uint32(&a, 1, rv); + rv = sum_int32(b, b_len, rv); + + return rv; +} +uint8_t sum_uint32(uint32_t* a, int len, uint8_t sum) +{ + union { + uint32_t u32; + uint8_t u8[4]; + } val; + + int next = len-1; + if (next < 0) return sum; + val.u32 = a[next]; + sum += val.u8[0]+val.u8[1]+val.u8[2]+val.u8[3]; + return sum_uint32(a, next, sum); +} + +uint8_t sum_int32(int32_t* a, int len, uint8_t sum) +{ + union { + int32_t d32; + uint8_t u8[4]; + } val; + + int next = len-1; + if (next < 0) return sum; + val.d32 = a[next]; + sum += val.u8[0]+val.u8[1]+val.u8[2]+val.u8[3]; + return sum_uint32(a, next, sum); +}