diff options
Diffstat (limited to 'src/core/io_moves.h')
| -rw-r--r-- | src/core/io_moves.h | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/core/io_moves.h b/src/core/io_moves.h index a49a38b..be43568 100644 --- a/src/core/io_moves.h +++ b/src/core/io_moves.h | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | STATIC uint8_t readmove(char); | 1 | STATIC uint8_t readmove(char); |
| 2 | STATIC uint8_t readmodifier(char); | 2 | STATIC uint8_t readmodifier(char); |
| 3 | STATIC int writemoves(uint8_t *, int, char *); | 3 | STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *); |
| 4 | 4 | ||
| 5 | STATIC uint8_t | 5 | STATIC uint8_t |
| 6 | readmove(char c) | 6 | readmove(char c) |
| @@ -38,18 +38,29 @@ readmodifier(char c) | |||
| 38 | } | 38 | } |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | STATIC int | 41 | STATIC int64_t |
| 42 | writemoves(uint8_t *m, int n, char *buf) | 42 | writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) |
| 43 | { | 43 | { |
| 44 | int i; | 44 | int i; |
| 45 | size_t len; | 45 | uint64_t len; |
| 46 | const char *s; | 46 | const char *s; |
| 47 | char *b; | 47 | char *b; |
| 48 | 48 | ||
| 49 | for (i = 0, b = buf; i < n; i++, b++) { | 49 | if (buf_size == 0) { |
| 50 | LOG("Error: cannot write moves to buffer of size 0.\n"); | ||
| 51 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 52 | } | ||
| 53 | |||
| 54 | for (i = 0, b = buf; i < n; i++, b++, buf_size--) { | ||
| 50 | s = movestr[m[i]]; | 55 | s = movestr[m[i]]; |
| 51 | len = strlen(s); | 56 | len = strlen(s); |
| 57 | if (len >= buf_size) { | ||
| 58 | LOG("Error: the given buffer is too small for " | ||
| 59 | "writing the given moves.\n"); | ||
| 60 | goto writemoves_error; | ||
| 61 | } | ||
| 52 | memcpy(b, s, len); | 62 | memcpy(b, s, len); |
| 63 | buf_size -= len; | ||
| 53 | b += len; | 64 | b += len; |
| 54 | *b = ' '; | 65 | *b = ' '; |
| 55 | } | 66 | } |
| @@ -58,5 +69,9 @@ writemoves(uint8_t *m, int n, char *buf) | |||
| 58 | b--; /* Remove last space */ | 69 | b--; /* Remove last space */ |
| 59 | *b = '\0'; | 70 | *b = '\0'; |
| 60 | 71 | ||
| 61 | return b - buf; | 72 | return buf_size; |
| 73 | |||
| 74 | writemoves_error: | ||
| 75 | *buf = '\0'; | ||
| 76 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 62 | } | 77 | } |
