~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to erts/emulator/beam/erl_md5.c

  • Committer: Bazaar Package Importer
  • Author(s): Erlang Packagers, Sergei Golovan
  • Date: 2006-12-03 17:07:44 UTC
  • mfrom: (2.1.11 feisty)
  • Revision ID: james.westby@ubuntu.com-20061203170744-rghjwupacqlzs6kv
Tags: 1:11.b.2-4
[ Sergei Golovan ]
Fixed erlang-base and erlang-base-hipe prerm scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
123
123
        BIF_ERROR(BIF_P, BADARG);
124
124
    }
125
125
    bin = new_binary(BIF_P, (byte *)NULL, 16);
126
 
    GET_BINARY_BYTES(bin, bytes);
 
126
    bytes = binary_bytes(bin);
127
127
    MD5Final(bytes, &context);
128
128
    BIF_RET(bin);
129
129
}
134
134
    Eterm bin;
135
135
    byte* bytes;
136
136
 
137
 
    bin = new_binary(BIF_P, (byte *)NULL, sizeof(MD5_CTX));
138
 
    GET_BINARY_BYTES(bin, bytes);
 
137
    bin = erts_new_heap_binary(BIF_P, (byte *)NULL, sizeof(MD5_CTX), &bytes);
139
138
    MD5Init((MD5_CTX *)bytes);
140
139
    BIF_RET(bin);
141
140
}
146
145
    byte* old_context;
147
146
    byte* new_context;
148
147
    Eterm bin;
 
148
    byte* temp_alloc = NULL;
149
149
 
150
 
    if (!is_binary(BIF_ARG_1)) {
 
150
    if ((old_context = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc)) == NULL) {
151
151
    error:
 
152
        erts_free_aligned_binary_bytes(temp_alloc);
152
153
        BIF_ERROR(BIF_P, BADARG);
153
154
    }
154
 
    GET_BINARY_BYTES(BIF_ARG_1, old_context);
155
 
    
156
155
    if (binary_size(BIF_ARG_1) != sizeof(MD5_CTX)) {
157
156
        goto error;
158
157
    }
159
 
    bin = new_binary(BIF_P, old_context, sizeof(MD5_CTX));
160
 
    GET_BINARY_BYTES(bin, new_context);
 
158
    bin = erts_new_heap_binary(BIF_P, old_context, sizeof(MD5_CTX), &new_context);
161
159
    if (!update((MD5_CTX *)new_context, BIF_ARG_2)) {
162
160
        goto error;
163
161
    }
 
162
    erts_free_aligned_binary_bytes(temp_alloc);
164
163
    BIF_RET(bin);
165
164
}
166
165
 
171
170
    byte* context;
172
171
    byte* result;
173
172
    MD5_CTX ctx_copy;
 
173
    byte* temp_alloc = NULL;
174
174
 
175
 
    if (!is_binary(BIF_ARG_1)) {
 
175
    if ((context = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc)) == NULL) {
176
176
    error:
 
177
        erts_free_aligned_binary_bytes(temp_alloc);
177
178
        BIF_ERROR(BIF_P, BADARG);
178
179
    }
179
 
    GET_BINARY_BYTES(BIF_ARG_1, context);
180
180
    if (binary_size(BIF_ARG_1) != sizeof(MD5_CTX)) {
181
181
        goto error;
182
182
    }
183
 
    bin = new_binary(BIF_P, (byte *)NULL, 16);
184
 
    GET_BINARY_BYTES(bin, result);
 
183
    bin = erts_new_heap_binary(BIF_P, (byte *)NULL, 16, &result);
185
184
    memcpy(&ctx_copy, context, sizeof(MD5_CTX));
 
185
    erts_free_aligned_binary_bytes(temp_alloc);
186
186
    MD5Final(result, &ctx_copy);
187
187
    BIF_RET(bin);
188
188
}
189
189
 
190
190
static int
191
 
update(MD5_CTX* context, Eterm obj)
 
191
update(MD5_CTX* context, Eterm iolist)
192
192
{
193
 
    unsigned char buf[1024];
194
 
 
195
 
    DECLARE_ESTACK(s);
196
 
    Eterm* objp;
197
 
    unsigned int csize = 0;
198
 
    char* cptr = buf;
199
193
    byte* bytes;
200
 
 
201
 
    goto L_jump_start;          /* Avoid push */
202
 
 
203
 
    while (!ESTACK_ISEMPTY(s)) {
204
 
        obj = ESTACK_POP(s);
205
 
    L_jump_start:
206
 
        if (is_list(obj)) {
207
 
        L_iter_list:
208
 
            objp = list_val(obj);
209
 
            obj = CAR(objp);
210
 
            if (is_byte(obj)) {
211
 
                if (csize >= sizeof(buf)) {
212
 
                    ASSERT(csize == sizeof buf);
213
 
                    MD5Update(context, buf, csize);
214
 
                    cptr = buf;
215
 
                    csize = 0;
216
 
                }
217
 
                *cptr++ = unsigned_val(obj);
218
 
                csize++;
219
 
            } else if (is_binary(obj)) {
220
 
                if (csize != 0) {
221
 
                    MD5Update(context, buf, csize);
222
 
                    cptr = buf;
223
 
                    csize = 0;
224
 
                }
225
 
                GET_BINARY_BYTES(obj, bytes);
226
 
                MD5Update(context, bytes, binary_size(obj));
227
 
            } else if (is_list(obj)) {
228
 
                ESTACK_PUSH(s, CDR(objp));
229
 
                goto L_iter_list; /* on head */
230
 
            } else if (!is_nil(obj)) {
231
 
                goto L_type_error;
232
 
            }
233
 
 
234
 
            obj = CDR(objp);
235
 
            if (is_list(obj)) {
236
 
                goto L_iter_list; /* on tail */
237
 
            } else if (is_binary(obj)) {
238
 
                if (csize != 0) {
239
 
                    MD5Update(context, buf, csize);
240
 
                    cptr = buf;
241
 
                    csize = 0;
242
 
                }
243
 
                GET_BINARY_BYTES(obj, bytes);
244
 
                MD5Update(context, bytes, binary_size(obj));
245
 
            } else if (!is_nil(obj)) {
246
 
                goto L_type_error;
247
 
            }
248
 
        } else if (is_binary(obj)) {
249
 
            if (csize != 0) {
250
 
                MD5Update(context, buf, csize);
251
 
                cptr = buf;
252
 
                csize = 0;
253
 
            }
254
 
            GET_BINARY_BYTES(obj, bytes);
255
 
            MD5Update(context, bytes, binary_size(obj));
256
 
        } else if (!is_nil(obj)) {
257
 
        L_type_error:
258
 
            DESTROY_ESTACK(s);
259
 
            return 0;
260
 
        }
261
 
    }
262
 
 
263
 
    if (csize != 0) {
264
 
        MD5Update(context, buf, csize);
265
 
    }
266
 
 
267
 
    DESTROY_ESTACK(s);
 
194
    Uint size = 64*1024;
 
195
    int r;
 
196
 
 
197
    if (is_binary(iolist)) {
 
198
        Uint bitoffs;
 
199
        Uint bitsize;
 
200
 
 
201
        ERTS_GET_BINARY_BYTES(iolist, bytes, bitoffs, bitsize);
 
202
        if (bitsize != 0) {
 
203
            return 0;
 
204
        }
 
205
        size = binary_size(iolist);
 
206
        if (bitoffs == 0) {
 
207
            MD5Update(context, bytes, size);
 
208
            return 1;
 
209
        }
 
210
    }
 
211
 
 
212
    bytes = erts_alloc(ERTS_ALC_T_TMP, size);
 
213
    r = io_list_to_buf(iolist, (char*) bytes, size);
 
214
    if (r >= 0) {
 
215
        size -= r;
 
216
    } else if (r == -2) {       /* Type error */
 
217
        erts_free(ERTS_ALC_T_TMP, (void *) bytes);
 
218
        return 0;
 
219
    } else {
 
220
        ASSERT(r == -1);        /* Overflow */
 
221
        erts_free(ERTS_ALC_T_TMP, (void *) bytes);
 
222
        if ((size = io_list_len(iolist)) < 0) { /* Type error */
 
223
            return 0;
 
224
        }
 
225
        bytes = erts_alloc(ERTS_ALC_T_TMP, size);
 
226
        r = io_list_to_buf(iolist, (char*) bytes, size);
 
227
        ASSERT(r == 0);
 
228
    }
 
229
    MD5Update(context, bytes, size);
 
230
    erts_free(ERTS_ALC_T_TMP, (void *) bytes);
268
231
    return 1;
269
232
}
270
233